1. 程式人生 > >if else 選擇機構 _多重if選擇機構_if選擇結構嵌套(綜合練習題——code)

if else 選擇機構 _多重if選擇機構_if選擇結構嵌套(綜合練習題——code)

三目運算 nbsp score 結構 一個數 clas 練習 ner java

  1 import java.util.*;
  2 class If01{
  3     public static void main(String[ ]args){
  4         //練習1:假如張三參加Java考試,判斷如果在95分以上則有獎勵
  5         Scanner input =new Scanner(System.in);
  6         System.out.print("請輸入張三的考試成績:");
  7         double score =input.nextDouble();
  8         /*if (score > 95) {
9 System.out.println("有獎勵"); 10 }*/ 11 12 //註意:在java中,當代碼塊只有一條語句時,則大括號可以省略,建議初學者編寫大括號 13 if (score > 95) 14 System.out.println("有獎勵"); 15 System.out.println("有懲罰");
16 /*= if (score > 95) { 17 System.out.println("有獎勵"); 18 } 19 System.out.println("有懲罰"); */ 20 } 21 } 22 class If02{ 23 public static void main(String[ ]args){
24 //練習2:假如張三參加Java考試,判斷如果在95分以上則有獎勵,否則發紅包 25 Scanner input = new Scanner(System.in); 26 System.out.print("請輸入成績: "); 27 double score = input.nextDouble(); 28 //第一種方法:使用兩個if塊完成的 29 /*if (score > 95){ 30 System.out.println("有獎勵"); 31 } 32 if (score <=95){ 33 System.out.println("發紅包"); 34 }*/ 35 36 //第二種方法:使用if else完成 37 /*if(score >95) { 38 System.out.println("有獎勵"); 39 } else{ 40 System.out.println("發紅包"); 41 }*/ 42 /*第三種方法:使用三目運算符完成 43 System.out.println(score >95 ? "有獎勵" : "發紅包");*/ 44 //第四種方法:使用多重if完成 45 if(score > 95){ 46 System.out.println("有獎勵"); 47 }else if(score <= 95){ 48 System.out.println("發紅包"); 49 } 50 } 51 } 52 class If03{ 53 public static void main(String[ ]args){ 54 //練習3:判斷一個數是否是三位的正數 55 Scanner input = new Scanner(System.in); 56 System.out.print("請輸入需要輸入的數:"); 57 int num = input.nextInt(); 58 if (num >100 && num < 1000) { 59 System.out.println(num +"是正數"); 60 } else{ 61 System.out.println(num +"不是正數"); 62 } 63 } 64 } 65 66 class If04{ 67 public static void main(String[ ]args){ 68 //練習4:輸入兩個數,分別存放在a和b中,判斷a+b的和大於100,則輸出a的值,否則輸出b的值 69 Scanner input = new Scanner(System.in); 70 System.out.print("請輸入數字 :"); 71 int a = input.nextInt(); 72 System.out.print("請輸入數字 :"); 73 int b = input.nextInt(); 74 System.out.println("a = " + a + "\nb = " +b); 75 if (a + b > 100){ 76 System.out.println("輸出:a =" +a); 77 }else{ 78 System.out.println("輸出b = " +b); 79 } 80 } 81 } 82 83 class If05{ 84 public static void main(String[ ]args){ 85 //練習5:判斷一個數是奇數還是偶數(使用多重if選擇結構完成此練習) 86 Scanner input = new Scanner(System.in); 87 System.out.print("請輸入你想要輸入的數字:"); 88 int num = input.nextInt(); 89 if(num % 2 == 0){ 90 System.out.println("偶數"); 91 } else{ 92 System.out.println("奇數"); 93 } 94 } 95 } 96 97 class If06{ 98 public static void main(String[ ]args){ 99 //練習6:根據輸入的年齡,輸出是老年(55以上)、中年(18-54)、青年(18-29)還是少年(0----17) 100 Scanner input = new Scanner(System.in); 101 System.out.print("請輸入年齡: "); 102 int age = input.nextInt(); 103 /*if (age >55 ){ 104 System.out.println("老年"); 105 }else if(age >= 18){ 106 System.out.println("青年"); 107 }else if(age >= 30){ 108 System.out.println("中年"); 109 }else if(age <18){ 110 System.out.println("少年"); 111 }*/ 112 113 //註意:多重if選擇結構種的條件順序可以顛倒,但是可能影響運行結果 114 if (age >55 ){ 115 System.out.println("老年"); 116 }else if(age >= 18 && age<=29){ 117 System.out.println("青年"); 118 }else if(age >= 30){ 119 System.out.println("中年"); 120 }else if(age <18 && age >=0){ 121 System.out.println("少年"); 122 }else{ 123 System.out.println("輸入年齡有誤!"); 124 } 125 } 126 } 127 128 class If07{ 129 public static void main(String[ ]args){ 130 //練習7:判斷一個字符,輸出是大寫字母、小寫字母還是數字字符 131 } 132 } 133 134 class If08{ 135 public static void main(String[ ]args){ 136 //練習8:判斷一個兩位數,是奇數還是偶數 137 Scanner input = new Scanner(System.in); 138 System.out.print("請輸入一個數:"); 139 int num = input.nextInt(); 140 //判斷,當前數num是否是兩位數 141 if (num >=10 && num<=99){ 142 //判斷,當前數是奇數還是偶數 143 if(num %2 ==0){ 144 System.out.println(num+"是偶數"); 145 }else{ 146 System.out.println(num+"是奇數"); 147 } 148 }else{ 149 System.out.println(num + "不是兩位數"); 150 } 151 } 152 } 153 154 class If09{ 155 public static void main(String[ ]args){ 156 //練習9:判斷一個三位正整數,是否是水仙花數 157 Scanner input = new Scanner(System.in); 158 System.out.print("請輸入數字: "); 159 int num = input.nextInt(); 160 //判斷當前數是否是三位正整數 161 if (num>=100 && num <=999){ //num = 123 162 //獲取各個位數 163 int bw = num / 100 , sw = num % 100 / 10 , gw = num % 10; 164 //計算各個位的立方和 165 int sum = bw*bw*bw+sw*sw*sw+gw*gw*gw; 166 //判斷,各個位立方和是否與當前數num相等 167 if (num == sum){ 168 System.out.println(num+"是水仙花數"); 169 }else{ 170 System.out.println(num+"不是水仙花數"); 171 } 172 }else{ 173 System.out.println(num+"不是三位正整數"); 174 } 175 } 176 }

if else 選擇機構 _多重if選擇機構_if選擇結構嵌套(綜合練習題——code)