Java複習筆記(四)流程控制語句

流程控制語句

1. 順序語句

語句: 使用分號分隔的代碼就是一個語句。
順序語句: 按照代碼順序從上往下執行所有的代碼就是順序語句。

class Demo1 {
    public static void main(String[] args) 
    {
        System.out.println("A");
        System.out.println("B");
        System.out.println("C");
        System.out.println("D");
    }
}

語句自上而下順序執行是一般語言的基本規則

2. if 判斷語句

if判斷語句的格式:

格式1:適用於一種情況使用。

if(判斷的條件){
    符合條件執行的代碼;      
}

格式2 : 適用於兩種情況下去使用的。

if(判斷條件){
    符合條件執行的代碼
}
else{
    不符合條件執行的代碼;
}

與 if else 結構相似的三元運算符
三元運算符的格式:布爾表達式?值1:值2;

public class Main{
    public static void main(String[] args){
        String answer;
        String str1 = "A";
        String str2 = "B";
        answer = str1==str2 ?"same":"different";
        System.out.print(answer);
    }
}

三元運算符的優點: 結構比較簡潔。
三元運算符的缺點: 符合條件必須返回一個結果,不能執行語句。

格式3 : 適用於兩種以上的情況下去使用的。

if(判斷條件1){
    符合條件1執行的代碼
}
else if(判斷條件2){
    符合條件2執行的代碼;
}
...
else if(判斷條件n){
    符合條件n執行的代碼;
}
else{
    不符合條件執行的代碼;
}

if 語句要注意的細節:

  1. 如果符合條件後只有一個語句需要執行,那麼可以省略大括號。但是建議不要省略,因爲結構不清晰。
  2. if語句的判斷條件後不能添加分號,否則待執行代碼就不會執行。
public class Main{
    public static void main(String[] args){
        int day = (int)(Math.random()*7)+1;
        if(day==0){
            System.out.println("星期天");
        }else if(day==1){
            System.out.println("星期一");
        }else if(day==2){
            System.out.println("星期二");
        }else if(day==3){
            System.out.println("星期三");
        }else if(day==4){
            System.out.println("星期四");
        }else if(day==5){
            System.out.println("星期五");
        }else if(day==6){
            System.out.println("星期六");
        }else{
            System.out.println("沒有對應的星期");
        }
    }
}

3. switch 判斷語句

switch 判斷語句的格式:

switch(表達式)
{
case 取值1:
    執行語句;
    break;
case 取值2:
    執行語句;
    break;
......
default:
    執行語句;
    break;
}

switch的使用規則:

(1)switch語句選擇的類型有四種:byte,short,int , char;JDK5.0增加了枚舉類型,JDK7.0增加了字符串類型的數據。
(2)case之間與default沒有順序。先判斷所有的case,沒有匹配的case執行default。
(3)switch語句停止的條件是遇到了break關鍵字或者結束switch語句的大括號。
(4)如果匹配的case或者default沒有對應的break,那麼程序會繼續向下執行,運行可以執行的語句,直到遇到break或者switch結尾結束。
(5)switch case中的值必須要與switch表達式的值具有相同的數據類型。而且case後跟的值必須是常量,不能跟變量。

//標準使用
public class Mian {
    public static void main(String[] args){
        String str1 = "chenjipayne";
        switch(str1){
            case "zhanchangyuan":
                System.out.println("大佬戰場原");
                break;
            case "luwenjun":
                System.out.println("壯漢陸文俊凌");
                break;
            case"chenjipayne":
                System.out.println("弱雞沉寂佩恩");
                break;
            default:
                System.out.println("查無此人");
                break;
        }
    }
}
//缺少break的情況
import java.util.Scanner;
public class Mian {
    public static void main(String[] args)
    {
        System.out.println("請輸入一個月份:");
        Scanner scanner = new Scanner(System.in);
        int month = scanner.nextInt();
        switch(month){
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
            default:
                System.out.println("沒有對應的季節");
        }
    }
}
//運行結果
請輸入一個月份:
6
夏天
秋天
冬天
沒有對應的季節

4. while & do while 循環語句

while循環語句的格式:

while(循環的條件){
    循環語句;
}

while循環語句要注意的事項:
(1)while循環語句一般是通過一個變量控制其循環的次數。
(2) while循環語句的循環體代碼如果只有一個語句的時候,那麼可以省略大括號。但是也是不建議省略。
(3) while循環語句的判斷條件後面不能跟有分號,否則待執行代碼就不會執行。

public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        while(count<5){
            System.out.println("hello chenjipayne!");
            count++;
        }
    }
}
//運行結果:
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!

do while循環語句格式:

do{
    循環語句;
}while(判斷條件);
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        do{
            System.out.println("hello chenjipayne!");
            count++;
        }
        while(count<5);
    }
}
//運行結果:
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!

while與do-while的區別:
(1)while循環語句是先判斷後執行循環語句的;
(2)do-while循環語句是先執行,後判斷。不管條件是否滿足至少會執行一次。

//當條件一開始就不滿足時,while()語句
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        while(count<0){
            System.out.println("hello chenjipayne!");
            count++;
        }
    }
}
//無輸出

//當條件一開始就不滿足時,do while()語句
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        do{
            System.out.println("hello chenjipayne!");
            count++;
        }
        while(count<0);
    }
}
//輸出:hello chenjipayne!

5. for 循環語句

for循環語句的格式:

for(初始化語句;判斷語句;循環後的語句){
    循環語句;
}

for循環語句 要注意的事項:
(1) for(;;)這種寫法 是一個死循環語句,相當於while(true);
(2) for循環語句的初始化語句只會執行一次,只是在第一次循環的時候執行而已。
(3) for循環語句的循環體語句只有一句的時候,可以省略大括號不寫。但是不建議省略。

//求1~100的整數和
public class Mian {
    public static void main(String[] args)
    {
        int and = 0;
        for(int i = 1; i <= 100; i++)
        {
            and += i;
        }
        System.out.println(and);
    }
}

for循環的括號中的語句除判斷條件外,全部可以寫到外部

//求1~100的整數和
public class Mian {
    public static void main(String[] args)
    {
        int and = 0;
        int i = 1;//初始化語句
        for( ; i <= 100; )
        {
            and += i;
            i++;//循環後的語句
        }
        System.out.println(and);
    }
}

for(;;)這種寫法用來製造死循環

public class Mian {
    public static void main(String[] args)
    {
        for( ; ; )
        {
            System.out.println("hello chenjipayne!");
        }
    }
}

6. break & continue 關鍵字

(1) break

break適用範圍:只能用於switch或者是循環語句中。

break作用:
1. break用於switch語句的作用是結束一個switch語句。
2. break用於循環語句中的作用是結束當前所在的循環語句。

筆試題目:break目前位於內層的for循環,如何才能讓break作用於外層 的for循環。
解:可以標記解決

public class Mian {
    public static void main(String[] args) 
    {
        //標記的命名只要符合標識符的命名規則即可。
        aaa:for(int j = 0 ; j<100 ; j++)// j=0 外層for循環
        {
            bbb:for(int i = 0 ; i< 100 ; i++)// i=0 內層for循環
            {
                System.out.println("hello world"); 
                break aaa;
            }       
        }
    }
}
(2) continue

continue適用範圍: continue只能用於循環語句。

continue作用:continue的作用是跳過本次的循環體內容,繼續下一次。

continue要注意的事項:
1. continue後面不能跟有其他語句,因爲永遠都無法執行到。
2. continue 也可以配合標記使用的。

//計算1-100的偶數總和.
public class Mian {
    public static void main(String[] args) 
    {   
        int sum = 0 ;
        for(int num = 1 ; num <= 100 ; num++){
            if(num%2!=0){
                continue;  //如果是奇數就跳過本次循環。
            }
            sum  = sum+num;
        }
        System.out.println("總和:"+ sum);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章