這樣寫也行
public class World { ? ?public static void main(String[] args) { ? int one = 20 ; ? ?String str=(one%2==0)?"one是偶數":"one是奇數"; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(str); } }
public class World { ? ?public static void main(String[] args) { ? int one = 20 ; ? ?String str=(one%2==0)?"one是偶數":"one是奇數"; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(str); } }
2015-05-24
舉報
2015-05-27
|開腦洞
2015-05-24
?:是三目運算符 ?寫的方式為 A?B:C; 首先判斷A為真還是為假,如果A為真則執行B語句,為假則執行C語句