if(condition)Statement
在此時的條件語句中的條件是需要用括號把它括起來。
其實,Java中的條件語句和C/C++中的是一樣的。而Java常常希望在某個條件為真的時候執行多條語句。此時,我們就會引入一個概念,那就是“塊模塊(block statement)”,具體格式如下,僅供參考:
{
statement1
statement2
...
}
就拿下面的例子,我們來試試上面的這個格式吧!
if(score>=90)
system.out.println("優"); 此時想要的輸出的這結果也是它的一種狀態!
}
塊模塊也被稱為是復合語句,可以在Java程序結構中原本只能放置一條簡單語句的地方放置多條語句。
比較常見的情況和例子中的差不多。
題目:輸入成績,讓程序自動判別分數對應的等級!
1 import java.util.Scanner; 2 public class TestSum { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 System.out.println("請輸入分數"); 9 Scanner Sc=new Scanner(System.in); 10 int Score=Sc.nextInt(); 11 12 if(Score>=90){ 13 System.out.println("優"); 14 }else if(Score>=70&&Score<90){ 15 System.out.println("良"); 16 }else if(Score>=60&&Score<70){ 17 System.out.println("中"); 18 }else{ 19 System.out.println("差"); 20 } 21 } 22 }