條件判斷語句(if-else)


if-else 語法

  • if-else 語法,只有一個語句塊被執行
  • if 和 else都是Java中的關鍵字
  • if 語法
  • 把 if-else 看做一個表達式,程序整體還是順序執行的

if (boolean值) {

  if 語句塊

} else {

  else 語句塊

}


 

例如:買包子,如果包子是新出爐的,那么再多買2個;否則就買3個。

public class IfElseBaozi {
    public static void main(String[] args) {
        int baozi = 3;

        boolean baoziGangChuLong = true;

        if(baoziGangChuLong) {
            baozi = baozi + 2;
            System.out.println("包子剛剛出籠,買了" + baozi + "個肉包子。");
        }else {
            System.out.println("買了" + baozi + "個肉包子。");
        }
    }
}

 

if-else 的嵌套

  • if-else 就是一個語句,可以是另一個語句的一部分,也可以是 if-else 的一部分,即嵌套。

 

求a,b,c三個數的最大數。

public class Example2 {
    public static void main(String[] args) {
        int a = 100;
        int b = 100;
        int c = 23;
        // 分這幾種情況:abc等大;a最大;b最大;c最大;ab等大並且最大;ac等大並且最大;bc等大並且最大。
        if (a==b&&b==c){ // a=b=c
            System.out.println("a,b,c等大,為"+a);
        }else {
            if(a>b){
                if(a>c){
                    System.out.println("a最大,為"+a);
                }else{ // a<=c
                    if (a==c){
                        System.out.println("a,c最大,為"+a);
                    }else{
                        System.out.println("c最大,為"+c);
                    }
                }
            }else{ // a <= b
                if (b>c){
                    if(a==b){
                        System.out.println("a,b最大,為"+a);
                    }else{
                        System.out.println("b最大,為"+b);
                    }
                }else{ // b<=c
                    if (b==c) {
                        System.out.println("b,c最大,為"+b);
                    }else{
                        System.out.println("c最大,為"+c);
                    }
                }
            }
        }
    }
}

if-else 的簡化

  • 如果if或者else的語句塊只有一個語句可以省略大括號。
  • 當else語句中存在if語句時,可以簡寫成 else if (條件) 的形式

if (boolean值)

  if 語句塊

else

  else 語句塊

 

if (boolean值) {

  if 語句塊

} else if (boolean值) {

  if 語句塊

} else {

  else 語句塊

}

public class OneStatementIfElse {
    public static void main(String[] args) {
        int a = 10;

        System.out.println("省略大括號");
        if (a > 0)
            System.out.println("a大於0");
        else
            System.out.println("a小於等於0");

        System.out.println("比較大小的完整的寫法");
        if (a > 0) {
            System.out.println("a大於0");
        } else {
            if (a == 0) {
                System.out.println("a等於0");
            } else {
                System.out.println("a小於0");
            }
        }

        System.out.println("比較大小的省略所有大括號的方法");
        if (a > 0)
            System.out.println("a大於0");
        else if (a == 0)
            System.out.println("a等於0");
        else
            System.out.println("a小於0");

        System.out.println("比較大小的代碼塊有多個語句的最優寫法");
        if (a > 0) {
            System.out.println("a大於0");
            System.out.println("買" + a + "個肉包子。");
        } else if (a == 0) {
            System.out.println("a等於0");
            System.out.println("不買肉包子了。");
        } else {
            System.out.println("a小於0");
            System.out.println("肉包子吃多了。");
        }

    }
}

簡化求最大數的程序

public class IfElseNestSimple {
    public static void main(String[] args) {
        int a = 10;
        int b = 99;
        int c = 99;

        System.out.println("a=" + a + ". b=" + b + ". c=" + c + ".");
        if (a == b && b == c) {
            System.out.println("a,b,c等大。");
        } else if (a > b && a > c) {
            System.out.println("a最大,為" + a);
        } else if (b > a && b > c) {
            System.out.println("b最大,為" + b);
        } else if (c > a && c > b) {
            System.out.println("c最大,為" + c);
        } else if (a == b && a > c) {
            System.out.println("a和b最大,為" + a);
        } else if (a == c && a > b) {
            System.out.println("a和c最大,為" + a);
        } else if (b == c && a < b) {
            System.out.println("b和c最大,為" + b);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM