if/else結構
基本樣式
if(布林值){ //true的執行區塊 } else{ //false的執行區塊 } exam. int a=10, b=20; if(a>b){ System.out.println("a比較大"); } else{ System.out.println("b比較大"); }
精簡型
若程式的執行區塊只有一行,則可以省略{}
int a=10, b=20; if(a>b) System.out.println("a比較大"); else System.out.println("b比較大"); //直接寫在後面也可以
階層式
if (布林值){ //執行區塊 } else if (布林值){ //執行區塊 } else if (布林值){ //執行區塊 } else{ //執行區塊 }
switch statement
if..else if…else 可以一直撰寫相關的條件,但又過於煩雜,所以出現了switch 的結構
switch(expression){ case 1 : //執行區塊 break; case 2 : //執行區塊 break; case 3 : //執行區塊 break; default : //執行區塊 }
Switch 條件只能是char、 byte、short、int,但 Java SE 7 新增了 String。比對時,其實會把 char、byte、 short 隱式轉成 int 再進行比對。
所以如果是long、float、double,則不能放入 switch 條件中,除非有強制轉換。
此語法中,case 的比對子不可以重複,必需是唯一的。
每個case後,不一定要有break。在沒有 break 的情形下,程式流程會直接灌到下一個 case 中。
default 是在無任何 case 符合 switch 要求的條件時所要執行的區塊,有如 if..else if ….else 的 else區塊。 請注意 default 只能有一個。
switch 回傳值
Java 12 新增 switch 的回傳值,不需要 break,語法更為精簡,但最後一定要有 default。
如果沒有 default ,則會出現 the switch expression does not cover all possible input values.
請參照如下代碼。
public class Main { public static void main(String[] args) { int day=3; String result=switch(day){ case 1 -> "星期一"; case 2 -> "星期二"; case 3 -> "星期三"; default -> "不是有效日期"; }; System.out.println(result); } }
比較運算子(Relational Operators)
== != < > <= >=
字串比較二內容是否相同,需使用 equals()方法。若使用 == , 則是比較二者是否為同一個物件
邏輯運算子
&& ||
&&真值表
只要有一個是false, 結果就是false
if(條件1 && 條件2)中, 若條件1為false, 則條件2不會執行
&& | true | false |
true | true | false |
false | false | false |
|| 真值表
只要有一個是true, 結果就是true
if(條件1 || 條件2)中, 若條件1為true, 則條件2不會執行
|| | true | false |
true | true | true |
false | true | false |
exam1
請輸入薪資 salary, 並計算所得稅及實領
0 <= salary < 20000 : 6%
20001 <= salary < 40000 : 7%
40001 <= salary < 60000 : 8%
60001 <= salary < 80000 : 9%
80001以上(含) : 13%
public class Tax { public static void main(String[] args) { double tax=0; Scanner in=new Scanner(System.in); System.out.print("Please input your salary : "); int salary=in.nextInt(); if(salary<20000)tax=0.06; else if (salary<40000)tax=0.07; else if (salary<60000)tax=0.08; else if (salary<8000)tax =0.09; else tax=0.13; System.out.printf("薪資 : %d, 所得稅 %.2f, 實領 : %.2f\n", salary, salary*tax, salary*(1-tax)); } } 結果 : Please input your salary : 50000 薪資 : 50000, 所得稅 4000.00, 實領 : 46000.00
exam2
空姐錄取標準
1. age<=45
2. height>=160
public class AndTest { public static void main(String[] args) { double tax=0; Scanner in=new Scanner(System.in); System.out.print("Please input age : "); int age=in.nextInt(); System.out.print("Please input height : "); int height=in.nextInt(); if(age<=45 && height >=160)System.out.println("錄取"); else System.out.println("不錄取"); } } 結果 : Please input age : 30 Please input height : 170 錄取 Please input age : 30 Please input height : 150 不錄取 Please input age : 50 Please input height : 170 不錄取 Please input age : 50 Please input height : 150 不錄取
exam3
年齡age >=65 或者低收入戶(salary<=10000) 者, 搭高鐵有優惠票價
public class Ticket {
public static void main(String[] args) {
double tax=0;
Scanner in=new Scanner(System.in);
System.out.print("Please input age : ");
int age=in.nextInt();
System.out.print("Please input salary : ");
int salary=in.nextInt();
if(age>=65 || salary >=10000)System.out.println("優惠票");
else System.out.println("普通票");
}
}
exam4
下面代碼中, 如果i++ >20 而且 j– <30, 則列印 True, 否則列印 False.
但i++是先處理再加1, 所以i++並沒有大於20. 第一條件為false時, 第二條件就不執行.
所以結果是 False, a 為19, J 為30
public class Test { public static void main(String[] args) { int i=20, j=30; if(i++>20 && j-- <30){ System.out.println("True"); } else{ System.out.println("False"); } } }