Java中條件語句和if-else的嵌套原則

在Java中,條件語句的格式爲:

if(condition)Statement

在此時的條件語句中的條件是需要用括號把它括起來。

其實,Java中的條件語句和C/C++中的是一樣的。而Java常常希望在某個條件爲真的時候執行多條語句。此時,我們就會引入一個概念,那就是“塊模塊(block statement)”,具體格式如下,僅供參考:

{
statement1
statement2
...
}

就拿下面的例子,我們來試試上面的這個格式吧!

if(score>=90)
system.out.println("優");   此時想要的輸出的這結果也是它的一種狀態!
}

塊模塊也被稱爲是複合語句,可以在Java程序結構中原本只能放置一條簡單語句的地方放置多條語句。

比較常見的情況和例子中的差不多。


題目:輸入成績,讓程序自動判別分數對應的等級!

import java.util.Scanner;
public class TestSum {
      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
        System.out.println("請輸入分數");
            Scanner Sc=new Scanner(System.in);
            int Score=Sc.nextInt();
            
            if(Score>=90){
                  System.out.println("優");
            }else if(Score>=70&&Score<90){
            System.out.println("良");
          }else if(Score>=60&&Score<70){
          System.out.println("中");
          }else{
            System.out.println("差");
          }
      }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章