Java第四週[JPA109、107、107_2、110 函數、副程式的運用

文章推薦指數: 80 %
投票人數:10人

第一題(JPA09)-變數範圍[函數、副程式的運用]:. 函數寫法(副程式). public static int fun() //int 有回傳值(要寫return) public static void test() //void 無回傳值.       Published LinkedwithGitHub Like Bookmark Subscribe #Java第四週[JPA109、107、107_2、110函數、副程式的運用、物件導向] ####複習用 ##開始解題(JPA09、7、7_2、10): ###第一題(JPA09)-變數範圍[函數、副程式的運用]: ####函數寫法(副程式) ```java=1 publicstaticintfun()//int有回傳值(要寫return) publicstaticvoidtest()//void無回傳值 ``` ![](https://i.imgur.com/6Zjcvwq.png) JPD0範本: ```java publicclassJPD01 { publicstaticintadder(__________________) { return_______________; } publicstaticintgameRating(ints,inta,inte) { returnadder(__________________); } publicstaticvoidmain(Stringargv[]) { intskill=6,action=9,excitment=8,result; result=gameRating(skill,action,excitment); System.out.print("Theratingofthegameis"); System.out.println(result); } } ``` 解答: ```java publicclassJPA09 { publicstaticintadder(intskill,intaction,intexcitement)//解 { returnskill+action+excitement;//解 } publicstaticintgameRating(ints,inta,inte) { returnadder(s,a,e);//解 } publicstaticvoidmain(Stringargv[]) { intskill=6,action=9,excitment=8,result; result=gameRating(skill,action,excitment); System.out.print("Theratingofthegameis"); System.out.println(result); } } ``` --- ###第二題(JPA07)-運動成績[物件導向]: ####函數寫法(副程式) ```java=1 publicstaticintfun()//int有回傳值(要寫return) publicstaticvoidtest()//void無回傳值 ``` ####物件導向 [參考資料1](http://kaiching.org/pydoing/java-guide/unit-12-static.html) [參考資料2](http://in.ncu.edu.tw/w3meng/train96/mengtrain96/lecture/TextBook_JAVAProgram.pdf) ####class用法 記得class要在外面宣告,而非放在JPA07的class裡 ![](https://i.imgur.com/yG7EIMn.png) JPD0範本: ```java publicclassJPD01 { publicstaticvoidmain(Stringargv[]) { intaction=1,skill=2,teamgame=3; System.out.println("Thebasketballgradeis"+Basketball.calGrade(_____________________)); System.out.println("Thebaseballgradeis"+_______________(________________________)); } } classBasketball { ...... { returna+s+t; } } classBaseball { ...... { return10+s+t; } } ``` 解答: ```java publicclassJPA07//JPA07class類別 { publicstaticvoidmain(Stringargv[]) { intaction=1,skill=2,teamgame=3; System.out.println("Thebasketballgradeis"+Basketball.calGrade(action,skill,teamgame)); System.out.println("Thebaseballgradeis"+Baseball.calGrade(skill,teamgame)); } } classBasketball//建立Basketball類別//建立類別,並宣告型態 { staticintcalGrade(inta,ints,intt)//設函數 { returna+s+t; } } classBaseball//建立Baseball類別 { staticintcalGrade(ints,intt)//設函數 { return10+s+t; } } ``` --- ###第三題(JPA07_2)-運動成績[物件導向的運用]: ####若要輸出abc的結果?要如何更改程式 ![](https://i.imgur.com/iTv70ne.png) 解答: ```java publicclassJPA072 { publicstaticvoidmain(Stringargv[]) { intaction=1,skill=2,teamgame=3; System.out.println("Thebasketballgradeis"+Basketball.calGrade(action,skill,teamgame)); Basketball.abc=3;//主程式設abc=3 System.out.println(Basketball.abc);//輸出abc=3 System.out.println("Thebaseballgradeis"+Baseball.calGrade(skill,teamgame)); System.out.println(Basketball.abc);//再次輸出abc的結果 } } classBasketball { staticintabc; //在籃球類別中宣告一個整數abc staticintcalGrade(inta,ints,intt) { returna+s+t; } } classBaseball { staticintcalGrade(ints,intt) { Basketball.abc=7;//在baseball類別中的副程式設abc=7 return10+s+t; } } ``` --- ###第四題(JPA010)-圖形面積[函數(副程式)運用]: ####函數寫法(副程式) ```java=1 publicstaticintfun()//int有回傳值(要寫return) publicstaticvoidtest()//void無回傳值 ``` ####面積公式 1.圓形[$r^2*pi$](pi=3.1415926) 2.三角形[底*高/2.0] 3.方形[長*寬] 4.梯形[(上底+下底)*高/2.0] *總面積相加即可 *double要記得/2.0 ![](https://i.imgur.com/1d7XzZO.png) ![](https://i.imgur.com/Igd3Nww.png) JPD0範本: ```java importjava.util.Scanner; publicclassJPD01 { publicstaticvoidmain(Stringargs[]) { doubletotalarea; System.out.printf("圓形面積為:%f\n",calCircle(5)); System.out.printf("三角形面積為:%f\n",________________); System.out.printf("方形面積為:%f\n",_______________); totalarea=__________________________________________; System.out.printf("此圖形面積為:%f\n",totalarea); } _____________calCircle(_____) { } __________calTriangle(_____________) { } __________calRectangle(______________) { } } ``` 解答: ```java importjava.util.Scanner; publicclassJPA10 { publicstaticvoidmain(Stringargs[]) { doubletotalarea; //圖形總面積 System.out.printf("圓形面積為:%f\n",calCircle(5)); System.out.printf("三角形面積為:%f\n",calTriangle(10,5)); System.out.printf("方形面積為:%f\n",calRectangle(5,10)); totalarea=calCircle(5)+calTriangle(10,5)+calRectangle(5,10); System.out.printf("此圖形面積為:%f\n",totalarea); //老師額外出題 System.out.printf("梯形面積為:%f\n",calTrapezoid(5,10,4)); } staticdoublecalCircle(intr)//圓形 { finaldoublepi=3.1415926; returnMath.pow(r,2)*pi; } staticdoublecalTriangle(intu,inth) //三角形 { returnu*h/2.0; } staticdoublecalRectangle(intm,intn) //方形 { return(double)m*n;//前面加(double),可以強制切換型態 } staticdoublecalTrapezoid(intu1,intu2,inth)//額外出題-梯形 { return(u1+u2)*h/2.0; } } ``` --- 最後編輯時間:2021/4/711:16pm. ######tags:`JAVA課堂學習``複習用``高科大` × Signin Email Password Forgotpassword or Byclickingbelow,youagreetoourtermsofservice. SigninviaFacebook SigninviaTwitter SigninviaGitHub SigninviaDropbox NewtoHackMD?Signup



請為這篇文章評分?