# Java第六週[JPA206~209 if與&&運用、Exception例外處理] #### 複習用 ## 開始解題(JPA206~9): ### 第一題(JPA206)-及格分數[基礎題(if判斷輸出+&&運用)]: * 分別顯示=>分別用if判斷 ![](https://i.imgur.com/0mDHRlu.png) 解答: ```java import java.util.*; public class JPA206 { static Scanner key = new Scanner(System.in); public static void main(String[] args) { test(); test(); test(); test(); } static void test() { int chi, eng, math,avg; //avg沒用到,不理他 System.out.print("Input Chinese score:"); chi = key.nextInt(); System.out.print("Input English score:"); eng = key.nextInt(); System.out.print("Input Math score:"); math = key.nextInt(); if(chi<60) { System.out.println("Chiese failed.");//可以分別輸出 } if(eng<60) { System.out.println("English failed."); } if(math<60) { System.out.println("Math failed."); } if((chi>60)&&(eng>60)&&(math>60)) //善用()連結&& { System.out.println("All Pass."); } } } ``` --- ### 第二題(JPA207)-三角形邊長判斷[基礎題(&&判斷運用)]: #### 構成[三角形]的存在條件 {題目要求條件1} 1. 兩邊和大於第三邊 [${a+b>c}$] 2. 三邊不能為0 [${a≠0、b≠0、c≠0}$] #### [三角形]類型 {題目要求條件2} 1. 直角三角形 ${a^2+b^2==c^2}$ [兩邊平方和**等於**第三邊] = 2. 鈍角三角形 ${a^2+b^2<c^2}$ [兩邊平方和**小於**第三邊] < 3. 銳角三角形 ${a^2+b^2>c^2}$ [兩邊平方和**大於**第三邊] > * 注意三角形公式為**平方** ![](https://i.imgur.com/fjdFr6y.png) 解答: ```java import java.util.*; public class JPA207 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { test(); test(); test(); test(); } static void test() { System.out.print("請輸入三個整數:"); //記得輸入三邊長 int a=keyboard.nextInt(); int b=keyboard.nextInt(); int c=keyboard.nextInt(); if(((a!=0)&&(b!=0)&&(c!=0))&&((a+b>c)&&(a+c>b)&&(b+c>a))) //條件1(符合三角形存在條件) { if((Math.pow(a,2)+Math.pow(b,2)==Math.pow(c,2))||(Math.pow(a,2)+Math.pow(c,2)==Math.pow(b,2))||(Math.pow(b,2)+Math.pow(c,2)==Math.pow(a,2))) { //條件2(符合三角形類型,並分別輸出) System.out.println("直角三角形"); } else if((Math.pow(a,2)+Math.pow(b,2)<Math.pow(c,2))||(Math.pow(a,2)+Math.pow(c,2)<Math.pow(b,2))||(Math.pow(b,2)+Math.pow(c,2)<Math.pow(a,2))) { System.out.println("鈍角三角形"); } else { System.out.println("銳角三角形"); } } else { System.out.println("不可以構成三角形"); } } } ``` --- ### 第三題(JPA208)-分級制度[基礎題(if判斷)]: * 注意題目要求,所以不能用if、else if、else ![](https://i.imgur.com/K1534Bi.png) 解答: ```java import java.util.*; class JPA208 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { test(); test(); test(); test(); test(); } public static void test() { System.out.println("Input:"); int a=keyboard.nextInt(); if(a>=90) //>=90 { System.out.println("You grade is A"); } else { if(a>=80) //題目要求<90~>=80 { System.out.println("You grade is B"); } else { if(a>=70) //題目要求<80~>=70 { System.out.println("You grade is C"); } else { if(a>=60) //題目要求<70~>=60 { System.out.println("You grade is D"); } else { System.out.println("You grade is F"); } } } } } } ``` --- ### 第四題(JPA208)-分級制度+顯示輸入錯誤[Exception例外處理1]: ![](https://i.imgur.com/2KaPTtR.png) ![](https://i.imgur.com/vER519N.png) #### 使用者輸入錯誤(非程式錯誤之例外) 有時候程式開發完,會遇到不預期的狀況 當使用者在輸入90時,意外輸入成kkk,就會產生這個狀況 並非程式錯誤,而是要做預防的動作 **提醒使用者輸入錯誤了,要再重新輸入一次** #### Exception例外 1. 輸入格式有誤 **Input Mismatch Exception** 2. 輸入/輸出例外(讀檔讀不到) **IO Exception** 3. 除0例外(12/0) **Arithmetic Exception** #### 例外[提示處理] {??放各種發生的例外} ```java //放在主程式 1.try { //放主要寫主程式的地方 } 2.catch(?? ie) //接住-寫提示的部分 { System.out.println("輸入錯誤,請輸入數值!"+ie"); } //放在副程式 3.throws ?? //丟出-傳送[輸入錯誤訊息] ``` * [cmd中文無法顯示解決](https://iter01.com/572486.html) ![](https://i.imgur.com/DYRcFr5.png) 解答: ```java import java.util.*; class JPA208_1 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { try { test(); test(); test(); test(); test(); } catch(InputMismatchException ie) //ie為例外訊息,名稱可自訂 { System.out.println("輸入錯誤,請輸入數值!"+ie); } } public static void test() throws InputMismatchException { System.out.println("Input:"); int a=keyboard.nextInt(); if(a>=90) { System.out.println("You grade is A"); } else { if(a>=80) { System.out.println("You grade is B"); } else { if(a>=70) { System.out.println("You grade is C"); } else { if(a>=60) { System.out.println("You grade is D"); } else { System.out.println("You grade is F"); } } } } } } ``` --- ### 第五題(JPA208)-分級制度+顯示除0錯誤[Exception例外處理2]: ![](https://i.imgur.com/ksK7QLB.png) ![](https://i.imgur.com/OCvuXz5.png) #### 例外[提示處理] {??放各種發生的例外} ```java //放在主程式 1.try { //放主要寫主程式的地方 } 2.catch(?? ie) //接住-寫提示的部分 { System.out.println("提示訊息"+ie"); } //放在副程式 3.throws ?? //丟出-傳送[輸入錯誤訊息] ``` ![](https://i.imgur.com/DG5tfer.png) 解答: ```java import java.util.*; class JPA208_2 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { try { test(); test(); test(); test(); test(); } catch(ArithmeticException ie) { System.out.println("除0錯誤,請修正"+ie); } } public static void test() throws ArithmeticException { System.out.println("Input:"); int a=keyboard.nextInt(); a=a/0; //除0運算方法(例外提示) if(a>=90) { System.out.println("You grade is A"); } else { if(a>=80) { System.out.println("You grade is B"); } else { if(a>=70) { System.out.println("You grade is C"); } else { if(a>=60) { System.out.println("You grade is D"); } else { System.out.println("You grade is F"); } } } } } } ``` --- ### 第六題(JPA209)-象限座標[if與&&運用]: #### 條件 1. 第一象限( x>0 && y>0 ) 2. 第二象限( x<0 && y>0 ) 3. 第三象限( x<0 && y<0 ) 4. 第四象限( x>0 && y<0 ) 5. x軸上( y==0 ) 6. y軸上( x==0 ) 7. 原點上[${x==0、y==0}$] ![](https://i.imgur.com/nabh3KZ.png) 解答: ```java import java.util.*; public class JPA209 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { test(); test(); test(); test(); } public static void test() { System.out.print("請輸入x座標:"); double x=keyboard.nextDouble(); System.out.print("請輸入y座標:"); double y=keyboard.nextDouble(); if((x>0)&&(y>0)) //一 { System.out.printf("<%.2f,%.2f>在第一象限\n",x,y); } else if((x<0)&&(y>0)) //二 { System.out.printf("<%.2f,%.2f>在第二象限\n",x,y); } else if((x<0)&&(y<0)) //三 { System.out.printf("<%.2f,%.2f>在第三象限\n",x,y); } else if((x>0)&&(y<0)) //四 { System.out.printf("<%.2f,%.2f>在第四象限\n",x,y); } else if((x!=0)&&(y==0)) //x軸 { System.out.printf("<%.2f,%.2f>在x軸上\n",x,y); } else if((y!=0)&&(x==0)) //y軸 { System.out.printf("<%.2f,%.2f>在y軸上\n",x,y); } else //原點 { System.out.printf("<%.2f,%.2f>在原點上\n",x,y); } } } ``` --- 最後編輯時間:2021/4/5 10:50pm. ###### tags: `JAVA課堂學習` `複習用` `高科大`