程式流程分類
1)順序執行:從上到下,從左到右(所有的代碼都執行)
2)分支判斷執行:根據條件進行判斷,條件成立執行一段代碼,條件不成立執行另外一段代碼(部分代碼執行,部分代碼不執行)
3)回圈迭代執行:反復執行一段代碼(代碼執行多次)
分支
java提供兩種陳述句:if陳述句(單分支)、switch陳述句(多分支)
if陳述句:
1)if(判斷條件:布爾運算式){ ,,,}
2)if(){ … } else{ … }
3)elseif if(){ … } elseif(){ … } elseif(){ … } else{ … } 多分支
package test;
import org.junit.Test;
//if陳述句的測驗
public class TestIf {
// Boolean isRainning = true; 成員變數
@Test
public void rainning() {
// 需求:下雨打傘
// 如果是成員變數使用包裝型別,
// 如果是區域變數使用基本型別
boolean isRainning = false;
if (isRainning) {
System.out.println("下雨打傘");
}
// 簡寫,只有當if陳述句后面只有一條陳述句是,才能省略大括號
// 不推薦,結構不清晰,號稱會出錯
if (isRainning)
System.out.println("下雨打傘");
System.out.println("執行完成");
//開發中最常見寫法
if (isRainning) {
System.out.println("喜水");
}else {
System.out.println("曬被子");
}
}
//需求:判斷電話號碼,輸出功能
@Test
public void elseIf() {
int phone = 999;
if(phone==110) {
System.out.println("警察");
}else if(phone==120) {
System.out.println("醫生");
}else if(phone==119) {
System.out.println("消防");
}else {
System.out.println("非法電話號碼");
}
}
}
switch 陳述句:
int n = 10; //整數
switch( n){
case 10:
…
case 20:
…
case 30:
…
default :
…
}
package test;
import org.junit.Test;
public class TestSwitch {
@Test
public void phone() {
//當switch只要某個條件成立,下面所以的條件不判斷,但是代碼會執行
//標準寫法,給每個分支最后加陳述句 break; 跳出
int phone = 120;
switch(phone) {
case 110: //判斷phone==110
System.out.println("警察");
break;
case 120:
System.out.println("醫生");
break;
case 119:
System.out.println("消防");
break;
default:
System.out.println("非法電話號碼");
break; //寫不寫都行
}
}
}
ATM
package test;
//必須選擇java.util(工具類)
import java.util.Scanner;
import org.junit.Test;
/*
* 需求:ATM輸入數字操作
* 1 取錢
* 2 存錢
* 3 余額
* 4 退出
*/
public class TestATM {
@Test
public void atm() {
//鍵盤輸入數字
System.out.println("請您輸入一個數字:");
Scanner scan = new Scanner(System.in);
//自動將輸入的內容轉換整數型別
//阻塞,等待用戶輸入,用戶沒有輸入時就死等
//回車繼續執行下面陳述句
int num = scan.nextInt();
System.out.println("鍵盤輸入:"+num);
switch(num) {
case 1:
System.out.println("取錢");
break;
case 2:
System.out.println("存錢");
break;
case 3:
System.out.println("余額");
break;
case 4:
System.out.println("退出");
break;
}
}
}
回圈
1)while 回圈
2)do-while 回圈
3)do 回圈
package test;
import org.junit.Test;
//回圈三種方式
public class TestLoop {
@Test //while回圈
public void whileLoop() {
//需求:列印0~9
int n = 0;
//當下面的判斷成立就執行回圈體,如果不成立結束回圈
while(n<10) {
System.out.println(n);
n++;
}
}
@Test //do while回圈
public void doLoop() {
int n = 0;
do {
System.out.println(n);
n++;
}while(n<10);
}
@Test //for回圈
public void for1() {
for(
int i=0; //第一句,只執行一次
i<10; //第二句,判斷條件,每次都判斷
i++) { //第四句,加一
System.out.println(i); //第三句,列印
}
}
@Test //雙重for回圈 i,j,k,m,n
public void for2() {
//利用*列印方框
for(int j=0; j<10; j++) { //外層回圈
for(int i=0; i<10; i++) { //內層回圈
System.out.print("*");
}
System.out.println(); //換行
}
}
@Test //99乘法表
public void for99() {
for(int j=1; j<10; j++) {
for(int i=1; i<j+1; i++) {
System.out.print(i+"*"+j+"="+i*j+"\t"); //tab
}
System.out.println();
}
}
}

死回圈
一直回圈無法退出
package test;
import org.junit.Test;
//死回圈
public class TestDeadLoop {
@Test
public void whileDead() {
while(true) {
System.out.println("*");
}
}
@Test
public void forDead() {
for(;;) {
System.out.println("*");
}
}
}
退出回圈
1)break 跳出
2)continue 跳過本次回圈
3)return 回傳
package test;
import org.junit.Test;
//退出回圈
public class TestExit {
@Test
public void exitLoop() {
int i = 0;
while(i<6) {
i++;
if(i==3) {
//break; //跳出回圈
//continue; //跳過本次回圈
return; //方法結束
}
System.out.println(i);
}
System.out.println("執行完成");
}
}

package test;
//必須選擇java.util(工具類)
import java.util.Scanner;
import org.junit.Test;
/*
* 需求:ATM輸入數字操作
* 1 取錢
* 2 存錢
* 3 余額
* 4 退出 return
*/
public class TestATM {
@Test
public void atm() {
//鍵盤輸入數字
Scanner scan = new Scanner(System.in);
//自動將輸入的內容轉換整數型別
//阻塞,等待用戶輸入,用戶沒有輸入時就死等
//回車繼續執行下面陳述句
while(true) {
System.out.println("請您輸入一個數字:");
int num = scan.nextInt();
System.out.println("鍵盤輸入:"+num);
switch(num) {
case 1:
System.out.println("取錢");
break;
case 2:
System.out.println("存錢");
break;
case 3:
System.out.println("余額");
break;
case 4:
System.out.println("退出");
//break;
return;
}
}
}
}
例外
try試圖/catch捕獲例外/finally釋放資源/
throw拋出例外/throws 接收例外
package test;
import org.junit.Test;
//例外處理,3個經典的錯誤
public class TestTryCatch {
@Test
public void tryCatch() {
//1. nullPointer例外
String s = null;
//System.out.println(s.length());
//2. zero被0除例外 n/0
//System.out.println(10/0);
//java.lang.ArithmeticException: / by zero
//3. 陣列越界 arr[5]
int[] arr = new int[5];
System.out.println(arr[5]);
//java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
}
}
package test;
import java.util.Scanner;
import org.junit.Test;
//例外處理,3個經典的錯誤
public class TestTryCatch {
@Test
public void err() {
//1. nullPointer例外
String s = null;
//System.out.println(s.length());
//2. zero被0除例外 n/0
//System.out.println(10/0);
//java.lang.ArithmeticException: / by zero
//3. 陣列越界 arr[5]
int[] arr = new int[5];
System.out.println(arr[5]);
//java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
}
@Test //對于可能出現的錯誤就需要使用try-catch包裹起來
public void tryCatch() {
//如果try中沒有出錯,不執行catch中內容
try {
int[] arr = {1,2,3,4,5};
System.out.println(arr[6]);
}catch(Exception e) { //把出現錯誤資訊存入到e物件實體中
System.out.println( e.getMessage());
//錯誤資訊比上面的詳細些
System.out.println( e.toString() );
e.printStackTrace(); //最詳細的錯誤資訊,一般沒必要
}
}
@Test //finally不管是否出錯,都釋放資源
public void tryCatchFinally() {
//列印鍵盤輸入內容(默認字串)
Scanner scan = new Scanner(System.in);
//Resource leak: 'scan' is never closed 對復雜變數關閉
try {
int num = scan.nextInt(); //不是整數"a"
System.out.println(num);
}catch(Exception ex) {
System.out.println( ex.toString() );
}finally {
//不管正確還是錯誤,這里的代碼都會執行
System.out.println("執行了我");
scan.close();
}
}
@Test //自己定義新的例外,并且拋出throw,方法接收例外throws
public void throwEx() throws Exception{
try {
int i = 10/0; //拋出例外
}catch(Exception e) {
System.out.println(e.toString());
//創建自己的例外,可以重新定義例外資訊
throw new Exception("分母不能為0例外,");
}
}
}
小結
1)流程控制結構:
a. 順序結構:從上到下,從左到右
b. 分支判斷結構:部分代碼執行,部分代碼不執行 if/else
c. 回圈迭代結構:反復執行一段代碼
d. 例外結構:出錯就跳過后面代碼直接執行catch中的代碼,都會執行finally中代碼
2)分支(單分支、多分支,分支的條件是互斥的,沒有交叉,只會執行其中某一個)
用來判斷,它讓我們程式有了生命力!程式的業務邏輯,
3)回圈(往復執行一件事情)
注意,在回圈體內一定要改變判斷條件!死回圈
while和do區別:while可能一次都不執行,do最少執行一次
for(第一句;第二句;第四句){第三句} 編譯器優化
4)例外 Exception
try-catch
try-catch-finally 釋放資源 Person p = new Person(); GC垃圾回收器(自動,它不受java開發者控制)
throw-throws,在catch中拋出例外throw產生新的例外,方法后面throws
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/290648.html
標籤:java
