# 程式設計 :::danger 教科書為[Head First Java](https://play.google.com/store/books/details/Kathy_Sierra_Head_First_Java?id=KXQrAQAAQBAJ&hl=zh-TW)(需自行上網購買) 免費PDF檔,歡迎取用 https://cloudflare-ipfs.com/ipfs/bafykbzacecbq6l37iww5jowhzffx3s5uunxrpnnuzlneafnsisbe7un7zcpes?filename=Kathy%20Sierra%2C%20Bert%20Bates%20-%20Head%20First%20Java-O%27Reilly%20Media%20%282005%29.pdf ::: :::warning ***[Hank的GitHub](https://github.com/hanktom?tab=repositories)*** ::: [***所有Eclipse開發環境教學皆可服用這篇文章***](https://www.kjnotes.com/devtools/80) [TOC] ## 程式設計期末考須知 - 考 - 問答題(1題) - 列印==圖形== - 排錯(會故意出錯,要你圈出來) - ==販賣機==題型 - 寫出程式結果 - 取合理變數名稱 - 思考邏輯(10~15分)(開放式題目) - 類別、local variables(區域變數) :::spoiler Hank的販賣機 ```java= package com.tom; import java.util.Scanner; public class Vending { //Drink a) 15 //Drink b) 25 //Drink c) 20 public static void main(String[] args) { int[] prices = {15, 25, 20, 22, 18}; System.out.println(prices.length); for (int i=0; i<prices.length; i++) { System.out.println(prices[i]); } Scanner scanner = new Scanner(System.in); int total = 0; boolean end = false; while(!end) { System.out.println("Please put your coin(1/5/10): Total:" + total); String s = scanner.next(); switch (s) { case "1": case "5": case "10": int n = Integer.parseInt(s); // total = total + n; total+=n; int i = 0; // i=i+1; i++; break; case "0": break; case "a": case "b": case "c": case "d": case "e": System.out.println(s.charAt(0)-97); int index = s.charAt(0) - 97; if (total >= prices[index]) { System.out.println("DON!"); total-=prices[index]; } else { System.out.println("BEEP!"); } break; default: System.out.println("Wrong answer!"); break; } /* try { int num = Integer.parseInt(s); switch (num) { case 1: case 5: case 10: total = total + num; break; case 0: end = true; break; default: break; } } catch (NumberFormatException e) { System.out.println("ah"); if (s.equals("a")) { System.out.println("A drink"); if (total >= aPrice) { total = total - aPrice; System.out.println("DON!"); } else { System.out.println("BEEP!"); } } } */ } System.out.println("END"); } } ``` ::: - 不考 - 物件導向 - Random(隨機發牌、猜數字) - 排序 - 資料結構 - 菱形不考 - 須知 - 盡量不要拼錯字 - 縮排縮清楚 - 寫整齊一點 - 不要刪題目 - 送分題 - 畫出程式結果圖形 ## 開發環境設定 ### Eclipse - 安裝 - [Adopt open jdk](https://adoptium.net/?variant=openjdk11) ---> X64 Temurin==11==(LTS) - [Eclipse](https://www.eclipse.org/downloads/) --->Downlord X86_64 - Text file encording ~路徑的部分大都是點[>]哦~ - Window/Preference/General/Workspace/==[UTF-8]== - 字型與大小 - Window/General/Appearance/Color and fonts/Java/Java Editor text font - 檔案 - File/New/Java Project取名/module-info取名/src右鍵/New/class(or package) - 在建class的時候記得勾public static void main >由於我常手殘關掉事件瀏覽器(就是顯示檔案的那個)所以特別提醒可以從Window/perspective/reset perspective來還原[name=BluePumpkin] ### IntelliJ - 安裝 - [intellij](https://www.jetbrains.com/idea/download/?fromIDE=#section=windows)--->community - 設定 - 中文字顯示錯誤 ![](https://i.imgur.com/oV553oK.png =60%x) - 到設置搜尋gradle,更改build and run為intellij IDEA >直接去看Hank的教學就好啦[name=BluePumpkin] ## 變數定義 - 整數 - short--->16bits - ==int--->32bits(較常用)== - long--->64bits - byte--->8bits - 浮點數 - float--->32bits - double--->64bits - 字元 - char--->16bits - 布林值 - boolean--->1bit - 陣列 - int ==[]== <font color=green>num</font> = new int[輸入陣列長度] - char <font color=green>type</font> ==[]==={'0','1','2'}; - ==[]== 放在<font color=green>名稱</font>前後皆可 ## 運算 - 比較運算:(輸出為==boolean==) - X > Y - X < Y - X == Y - X >= Y - X <= Y - X != Y - 數值運算:(輸出為==int==) - X + Y - X - Y - X * Y - X / Y (取商數) - X % Y (取餘數) - 邏輯運算:(輸出為==boolean==) - 可參考[計算機概論(一)](/98nJfhEKQ8az6dWPb8MsoA)Chapter 4. - &&--->AND - !!--->OR ## ==class==<small>A.K.A設計</small> - `public class classname{}` - 為整理主程式而衍生出的副程式 - 內容包含 1. <ruby>屬性<rp>(</rp><rt>Filed</rt><rp>)</rp></ruby> - ex:`int point =1;` - instance variable 2. <ruby>方法/功能<rp>(</rp><rt>Method</rt><rp>)</rp></ruby> - 位置為回傳資料類型e.g.`public booiean isMax{}`回傳為布林值 - 不回傳`public void voidname{}` 3. <ruby>建構子<rp>(</rp><rt>Constuctor</rt><rp>)</rp></ruby> - 當主程式使用new時,生成一個建構子 - 建構子的方法名稱要等於類別名稱 - e.g.`public Dice(){}` - 不能有void之類的 - 建構子通常用於初始化 ## Other - 螢幕錄影及截圖 - [ShareX](https://getsharex.com/) ## 沒人要看的課本導論 ::: spoiler Chapter 1. - 導論 - write-once, run-anywhere - source--->compiler--->output~bytecode~--->JVM - JVM--->**J**ava **V**irture **M**achines - Java history - Java 1.02--->250classes - Java 1.1--->500classes - Java 2(version1.2~1.4)--->2300classes - Java 5.0(version 1.5 and up)--->3500classes - about files ![Imgur](https://i.imgur.com/6hMxSYx.png) - class - method - statements ![Imgur](https://i.imgur.com/o8EDsEi.png) ::: ## CODING ### Tips - 縮排按`Tab` - 句尾結束加==分號(;)== - `Alt`+`/`可自動列出函式庫 - 字串前後加 ==""== - `shift`+`F6` 可選取所有名稱相同的變數並改名 - `Ctrl`+`Shift` +`F10`=執行 - 浮點數預設為**double** - 此為預編譯機制 - 數字尾加==f==可轉為float - `ex: 67.3資料型態為double;67.3f則為float` ### 引用函式 - import java.函式庫名稱.函式名稱; - import java.函式庫名稱.==*==;--->(米字號代表全函式庫皆引入) ### 螢幕輸出 - 輸出不換行 ```java= System.out.print(); ``` - 輸出並換行 - 若()內空白則為換行字元 ```java= System.out.println(); ``` - 特例 ```java= j=1; char type[]={'a'}; System.out.print(j+type[0]); //輸出的資料型態為數字:1+97(ASCII碼) System.out.print(j+""+type[0]); //輸出的資料型態為字串數字混合:1a ``` ### 產生亂數 - 浮點數 ```java,mediawiki= double d = Math.random(); //隨機出現0.0~1.0的浮點數(資料型態為double) ``` - 整數(需先匯入函式) ```java,mediawiki= import java.util.Random //匯入Random函式 Random r = new Random(); System.out.println(r.nextInt(6)); //輸出為亂數0~5 System.out.println(r.nextInt(6)+1); //輸出為亂數1~6 ``` ### 進位法 - 函式floor用法 ```java,mediawiki= float n = 3.4 System.out.println(Math.floor(n)); (無條件捨去) //輸出為3.0 System.out.println(Math.floor(n+0.5)); (四捨五入) //輸出為3.0 ``` - 函式ceil用法 ```java,mediawiki= float n = 3.2 System.out.println(Math.ceil(n)); (無條件進位) //輸出為4.0 ``` ### 條件 - if (Boolean) - 條件區可用`!`--->NOT ```java,mediawiki= if (條件){ 執行程式碼; }else{ code } ``` - if else ### 迴圈 - while (Boolean) ```java,mediawiki= boolean flag = true; //先定義flag為布林值且為true while (flag){ 執行程式碼; //若要結束迴圈 flag = false; } ``` - for (起始值;終點值;漸進值) - i++意即i=i+1(i- -同理) ```java,mediawiki= for (int i = 0;i<3;i++;){ 執行程式碼; } ``` ### 迴圈控制<small>[補充](https://blog.yslifes.com/archives/630#google_vignette)</small> - break--->跳出迴圈 ### switch - 每個case加break,否則會接著執行下一個case(default則執行完直接結束迴圈) - n也可使用字串(case後記得加"") ```java= int n=0; switch(n){ case 1://n==1時執行 break; case 2://n==2時執行 break; default://n不在case範圍中時執行 break; } ``` ### try / catch - 用於檢測輸入錯誤的資料型態 ```java= try{ int num =Integer.parseInt(s); }catch (NumberFormatException e){ System.out.println("ah!") } ``` ### equals - 用於比較兩字串是否相同 ```java= String s = "a"; String a = "a"; if( s.equals(a)){ System.out.println("true"); }else { System.out.println("false"); } ``` ### <ruby>三元運算式<rp>(</rp><rt>Ternary expression</rt><rp>)</rp></ruby> - 給予一個回傳布林值的條件,如果回傳為true則執行冒號前;false則執行冒號後 - e.g:`String s=dice.isMax() ? "*":"";`<br>如果dice點數為6則s為"*";不為6則為"" ## LABs ::: spoiler Lab 0x0 終極密碼 ### 解法一 >~~今晚~~現在寫完[name=BluePumpkin] ```java= import java.util.Random; import java.util.Scanner; public class guessnumber0to100 { public static void main(String[] args) { Random random=new Random(); int secret= random.nextInt(100)+1; System.out.println(secret); int num = 0; int min = 0; int max = 100; int c = 0; while (num!=secret){ c=c+1; if (c>5){ break; }else{ System.out.println("please enter number:"+min+"~"+max); Scanner scanner = new Scanner(System.in); String s = scanner.next(); num = Integer.parseInt(s); if (num<secret){ min=num; }else{ max=num; } } } if (c<5){ System.out.println("BINGO!!!"); }else{ System.out.println("LOSER!!!"); } } } ``` >結束這回合[name=BluePumpkin][time=Tue, Nov 2, 2021 4:53 PM] ### 解法二 ```java= import java.util.Random; import java.util.Scanner; public class Guess1TO100 { public static void main(String[] args) { Random random = new Random(); int secret = random.nextInt(100) + 1; System.out.println(secret); System.out.println("Please enter number 0~100: "); int max = 100; int min = 0; boolean end = false; while (!end) { Scanner scanner = new Scanner(System.in); String s = scanner.next(); int num = Integer.parseInt(s); if (num > secret) { System.out.println("enter number:"+min +"~"+num); max = num; } if (num < secret){ System.out.println("enter number:"+num+"~"+max); min = num; } if (num == secret){ System.out.println("Bomb"); end = true; } } } } ``` >[name=Aralia1112][time=Tue, Nov 2, 2021 6:17 PM] ### 解法三 ```java= import java.util.Random; import java.util.Scanner; public class guessnum { public static void main(String[] args) { Random random = new Random(); int secret = random.nextInt(100)+1; System.out.println(secret); int max = 100; int min = 1; System.out.println("max: "+max+" min: "+min); System.out.println("Please enter num:"); while(true){ Scanner scanner = new Scanner(System.in); String s = scanner.next(); int num = Integer.parseInt(s); if( num > secret){ max = num; System.out.println("Smaller "+"max: "+max+" min:"+min); } else if( num < secret){ min = num; System.out.println("Bigger "+"max: "+max+" min:"+min); } else{ System.out.println("Bingo"); break; } } } } ``` >[name=frankshicar][time=Wed, Nov 3, 2021 3:30 PM] ::: ::: spoiler Lab 1x0 StarSquare ![StarSquare](https://i.imgur.com/C5CroRF.png) ### 解法一<small>by BluePumpkin</small> ```java= public class StarSquare { public static void main(String[] args) { for (int i=0;i<10;i++) { for (int j=0;j<20;j++) { if (i==1||i==8) { if (j==0||j==19) { System.out.print("*"); }else{ System.out.print(" "); } }else { if (j==1||j==18) { if (i==0||i==9){ System.out.print("*"); }else { System.out.print(" "); } } else { System.out.print("*"); } } } System.out.println(); } } } ``` >結束這回合[name=BluePumpkin][time=Tue, Nov 9, 2021 2:39 PM] ```java= package main; public class StarSquare { public static void main(String[] args) { for (int i=0;i<10;i++) { for (int j=0;j<20;j++) { if (i==1||i==8||j==1||j==18) { if (i==0||i==9||j==0||j==19) { System.out.print("*"); }else{ System.out.print(" "); } }else{ System.out.print("*"); } } System.out.println(); } } } ``` >這應該更好理解[name=BluePumpkin][time=Wed, Nov 10, 2021 3:04 PM] ### 解法二(可隨意輸入數字產生圖形)<small>by frankshicar</small> ```java= package com.frank; import java.sql.SQLOutput; import java.util.Scanner; public class Stars { public static void main(String[] args) { System.out.print("Please enter mun:"); Scanner scanner = new Scanner( System.in); String a = scanner.next(); int lon = Integer.parseInt(a); for (int i = 0; i <= lon; i++) { for (int n = 0; n <= lon; n++) { if (i == 0 || i == lon) { System.out.print("*"); } else if (i == 1 || i == lon-1) { if (n == 0 || n == lon) System.out.print("*"); else System.out.print(" "); } else { if (n == 1 || n == lon-1) System.out.print(" "); else System.out.print("*"); } } System.out.println(); } } } ``` >結束這回合[name=frankshicar][time=Tue, Nov 9, 2021 3:20 PM] 解法三 ```java= public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { if (i == 1 || i == 8) { if (j == 0 || j == 19) { System.out.print("*"); } else { System.out.print(" "); } } else { if(i>=2 || i<=7 ){ if(j==1 || j==18){ System.out.print(" "); }else{ System.out.print("*"); } }else{ System.out.print("*"); } } } System.out.println(); } } } ``` >[name=Aralia1112][time=Wed , Nov 10,2021 4:09 PM] ::: ::: spoiler Lab 1x1 StarLine ![StarLine](https://i.imgur.com/MYbh8yx.png) ### 解法一<small>by BluePumpkin</small> ```java= public class StarLine { public static void main(String[] args) { for (int i=0;i<10;i++){ for (int j=0;j<20;j++){ if (2*(9-i)==j){ System.out.print("*"); }else { System.out.print(" "); } } System.out.println(); } } } ``` >[name=BluePumpkin][time=Tue, Nov 9, 2021 3:00 PM] ### 解法二<small>by Aralia1112</small>(左斜線) ```java= public class leftline { public static void main(String[] args) { for(int i=0;i<10;i++){ for(int j=0;j<20;j++){ if(j==2*i){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } } ``` >[name=Aralia1112][time=Sun, Nov 14, 2021 10:14 AM] ### 解法三<small>by Aralia1112</small>(右斜線) ```java= public class rightline { public static void main(String[] args) { for(int i=0;i<10;i++){ for (int j=0;j<20;j++){ if(j==19-2*i){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(" "); } } } ``` >[name=Aralia1112][time=Sun, Nov 14, 2021 10:16 AM] ### 解法四<small>by frankshicar</small>(右斜線) ```java= package test; public class test { public static void main(String[] args) { for( int n =10 ; n >=0 ; n--) { for (int i = n-1 ; i >= 0; i--) { System.out.print(" "); } System.out.println("*"); } } } ``` >[name=frankshicar][time=Sun, Nov 14, 2021 10:16 AM] ### 解法五<small>by frankshicar</small>(左斜線) ```java= package test; public class test { public static void main(String[] args) { for( int n = 0 ; n <=10 ; n++) { for (int i = n-1 ; i >= 0; i--) { System.out.print(" "); } System.out.println("*"); } } } ``` >[name=frankshicar][time=Sun, Nov 14, 2021 10:16 AM] ::: :::spoiler Lab 1x2 StarX ![StraX](https://i.imgur.com/bhDDrnM.png) ### 解法一<small>by BluePumpkin</small> ```java= public class StarX { public static void main(String[] args) { for (int i=0;i<10;i++){ for (int j=0;j<20;j++){ if (2*i==j||2*i==19-j){ System.out.print("*"); }else { System.out.print(" "); } } System.out.println(); } } } ``` >[name=BluePumpkin][time=Tue, Nov 9, 2021 3:11 PM] ### 解法二<small>by Aralia1112</small> ```java= public class spaceX { public static void main(String[] args) { for (int i=0;i<10;i++){ for(int j=0;j<20;j++){ if(j==2*i || j==19-2*i){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(" "); } } } ``` >[name=Aralia1112][time=Sun, Nov 14, 2021 10:30 AM] ### 解法三<small>by frankshicar</small> ```java= package test; public class test { public static void main(String[] args) { for( int n = 0 ; n <= 10 ; n++){ for( int i = 0 ; i <= 10 ; i++){ if( i == n || i == 10 - n) { System.out.print("*"); } else{ System.out.print(" "); } } System.out.println(); } } } ``` >[name=frankshicar][time=Sun, Nov 14, 2021 10:30 AM] ::: :::spoiler Lab 1x3 StarArrow ![alt text](https://i.imgur.com/rIjYl3a.png "StarArrow") ### 解法一 ```java,mediawiki= public class StarArrow { public static void main(String[] args) { for (int i=0;i<11;i++){ for (int j=0;j<21;j++){ if (i<6){ if (2*i==j){ System.out.print("*"); }else { System.out.print(" "); } }else { if (2*i==20-j){ System.out.print("*"); }else{ System.out.print(" "); } } } System.out.println(); } } } ``` >[name=BluePumpkin][time=Tue, Nov 9, 2021 3:18 PM] ### 解法二 ```java= public class Arrow { public static void main(String[] args) { for(int i=0;i<9;i++){ for(int j=0;j<19;j++){ if(i<5){ if(j==i*2){ System.out.print("*"); }else{ System.out.print(" "); } }else{ if(j==-2*i+16){ System.out.print("*"); }else{ System.out.print(" "); } } } System.out.println(); } } } ``` >[name=Aralia1112][time=Sun, Nov 14, 2021 11:50 AM] ::: :::spoiler Lab 2x0 販賣機 ### 解法一<small>by BluePumpkin</small>. ```java= package main; import java.util.Scanner; public class sellingmachine { public static void main(String[] args) { int milktea = 20; int cola = 15; int juice = 10; System.out.println("請投幣1元"); Scanner oneDollar = new Scanner(System.in); int one = oneDollar.nextInt(); System.out.println("請投幣5元"); Scanner fiveDollars = new Scanner(System.in); int five = fiveDollars.nextInt(); System.out.println("請投幣10元"); Scanner tenDollars = new Scanner(System.in); int ten = tenDollars.nextInt(); int sum = one+5*five+10*ten; System.out.println(sum); int s = 0; int total = 0; while (s!=4){ System.out.println("請選擇商品。奶茶(20元)請按1,可樂(15元)請按2,果汁(10元)請按3,選擇完畢請按4"); Scanner select = new Scanner(System.in); s = Integer.parseInt(select.next()); if (s!=1){ if (s!=2){ if (s!=3){ break; }else { total = total + 10; } }else{ total = total + 15; } }else { total = total + 20; } int back= sum-total; if (back<0){ System.out.println("請補差額"); break; } System.out.println("餘額:"+back); } int back= sum-total; System.out.println(back); if (back >= 0){ System.out.println("找回10元"+back/10+"個"); System.out.println("找回5元"+back%10/5+"個"); System.out.println("找回1元"+back%10%5+"個"); }else{ System.out.println("請補10元"+(back/10)*(-1)+"個"); System.out.println("請補5元"+back%10/5*(-1)+"個"); System.out.println("請補1元"+back%10%5*(-1)+"個"); } } } ``` ### 解法二(switch實作)<small>by BluePumpkin</small> ```java= import java.util.Scanner; public class SellingSwitch { public static void main(String[] args) { int n = 0; int total = 0; boolean end = false; while (!end) { System.out.println("please put your coin(1/5/10): total=" + total); Scanner scanner = new Scanner(System.in); String s = scanner.next(); int coin = Integer.parseInt(s); switch (coin) { case 0: end = true; break; case 1: case 5: case 10: total = total + coin; break; } } end = false; System.out.println("total=" + total); while (!end) { if (total >= 0) { System.out.println("(1)奶茶20/(2)可樂15/(3)多多10/(0)選擇完畢"); System.out.println("please choose your goods:"); Scanner choose = new Scanner(System.in); int c = choose.nextInt(); switch (c) { case 1: total = total - 20; System.out.println("剩餘"+total); break; case 2: total = total - 15; System.out.println("剩餘"+total); break; case 3: total = total - 10; System.out.println("剩餘"+total); break; default: end = true; break; } } else { end = true; } } if (total >= 0) { System.out.println("找零十元" + total / 10 + "個/五元" + total % 10 / 5 + "個/一元" + total % 10 % 5 + "個"); } else { total = total * -1; System.out.println("請補差額" + total / 10 + "個/五元" + total % 10 / 5 + "個/一元" + total % 10 % 5 + "個"); } } } ``` ### 解法三(第一版)<small>by Aralia1112</small> ```java= import java.util.Scanner; public class drink { public static void main(String[] args) { int aPrice=15; int bPrice=25; int cPrice=30; Scanner scanner =new Scanner(System.in); int total = 0; boolean end = false; while (!end){ System.out.println("Please put your coin (1/5/10)"); String s =scanner.next(); switch (s) { case "1": case "5": case "10": int num = Integer.parseInt(s); total = total + num; System.out.println("total: "+total); break; case "0": end = true; System.out.println("final total: "+total); break; case "a": if(total>aPrice){ total =total-aPrice; System.out.println("DOM!"); System.out.println("total: "+total); }else{ System.out.println("餘額不足,請補差價"); } break; case "b": if(total>bPrice){ total =total-bPrice; System.out.println("DOM!"); System.out.println("total: "+total); }else{ System.out.println("餘額不足,請補差價"); } break; case "c": if(total>cPrice){ total =total-cPrice; System.out.println("DOM!"); System.out.println("total: "+total); }else{ System.out.println("餘額不足,請補差價"); } break; default: System.out.println("錯誤輸入,請再輸入一次!"); break; } }if(total>=0){ System.out.println("找零10元"+(total/10)+"個 "+"5元"+(total-(total/10)*10)/5+"個 "+"1元"+(total%10%5/1)+"個"); } } } ``` ### 解法三(第二版)<small>by Aralia1112</small> ```java= import java.util.Scanner; public class Changes { public static void main(String[] args) { int aPrice=15; int bPrice=25; int cPrice=30; int dPrice=20; int ePrice=35; int [] prices={15,25,30,20,35}; //System.out.println(prices.length); for(int i=0;i<prices.length;i++) { System.out.println(prices[i]); } Scanner scanner =new Scanner(System.in); int total = 0; boolean end = false; while (!end){ System.out.println("Please put your coin (1/5/10)"); String s =scanner.next(); switch (s) { case "1": case "5": case "10": int num = Integer.parseInt(s); total = total + num; System.out.println("total: "+total); break; case "0": end = true; System.out.println("final total: "+total); break; case "a": case "b": case "c": case "d": case "e": //System.out.println(s.charAt(0)-97); int index=s.charAt(0)-97; if(total>=prices[index]){ total =total-prices[index]; System.out.println("DOM!"); System.out.println("total: "+total); }else{ System.out.println("餘額不足,請補差價"); System.out.println("total: "+total); } break; default: System.out.println("錯誤輸入,請再輸入一次!"); System.out.println("total: "+total); break; } }if(total>=0){ System.out.println("找零10元"+(total/10)+"個 "+"5元"+(total-(total/10)*10)/5+"個 "+"1元"+(total%10%5/1)+"個"); } } } ``` ::: :::spoiler Lab 3x0擲骰子10次並記錄點數出現次數 ### 解法一 ```java= package test; import java.util.Random; public class dice { public static void main(String[] args) { int[] count = new int[6]; for ( int i = 0; i < 10; i++ ) { Random random = new Random(); int point = random.nextInt(6)+1; count[point-1]++; System.out.println(point); } System.out.println(); for ( int y = 0; y < 6; y++ ) { System.out.println(y+1+"點出現過"+count[y]+"次"); } } } ``` >[name=frankshicar][time=Sun, Dec 5, 2021 11:50 PM] ```java= public class DiceRoller { public static void main(String[] args) { int counter[]=new int[6]; for (int i = 0; i < 10; i++) { //roll dice 10 times Dice dice=new Dice(); System.out.print(dice.point); //print the point System.out.println(dice.isMax()? "*":""); //is the point==6? counter[dice.point-1]+=1; //counter point times+1 } for (int i = 0; i < 6; i++) { //print point times System.out.println(i+1+"點出現"+counter[i]+"次"); } } } //class Dice here import java.util.Random; public class Dice { int point=1; // int a 變數 named point public Dice (){ // when new a Dice, roll its point Random random=new Random(); point= random.nextInt(6)+1; } public boolean isMax(){ // if point==6 return true;if not return false if (point==6){ return true; }else { return false; } } } ``` >[name=BluePumpkin] ::: :::spoiler Lab 3X1 OneCardGame ```java= import java.util.Random; public class OneCardGame { public static void main(String[] args) { char[] types={'c','d','h','s'}; Random random=new Random(); //computer int computer=random.nextInt(52); int compoint=computer%13+1; int comtype=computer/13; System.out.println(types[comtype]+""+compoint+"="+computer); //player int player= random.nextInt(52); int ppoint=player%13+1; int ptype=player/13; System.out.println(types[ptype]+""+ppoint+"="+player); //V.S. if (ppoint>compoint || (ppoint==compoint && types[ptype]>types[comtype])){ System.out.println("YOU WIN!!"); }else { System.out.println("YOU LOOSE!!"); } } } ``` ::: ### Midexam :::spoiler EX1.猜數字0~100 ### 解法一<small>by BluePumpkin</small> ```java= import java.util.Random; import java.util.Scanner; public class guessnumber0to100 { public static void main(String[] args) { Random random=new Random(); int secret= random.nextInt(100)+1; System.out.println(secret); int num = 0; int min = 0; int max = 100; int c = 0; while (num!=secret){ c=c+1; if (c>5){ break; }else{ System.out.println("please enter number:"+min+"~"+max); Scanner scanner = new Scanner(System.in); String s = scanner.next(); num = Integer.parseInt(s); if (num<secret){ min=num; }else{ max=num; } } } if (c<5){ System.out.println("BINGO!!!"); }else{ System.out.println("LOSER!!!"); } } } ``` ::: :::spoiler EX2.StarBlock ### 解法一<small>by BluePumpkin</small> ```java= package com.fju; import java.util.Scanner; public class StarBlock { public static void main(String[] args) { System.out.println("enter first number:"); Scanner first = new Scanner(System.in); int one = first.nextInt(); System.out.println("enter second number:"); Scanner second = new Scanner(System.in); int two = second.nextInt(); for (int i =0;i<two-one;i++){ for (int j=0;j<20;j++){ if (j<one||j>two) { System.out.print("0"); }else{ System.out.print("*"); } } System.out.println(); } } } ``` ::: :::spoiler EX3.StarPyramid ### 解法一<small>by BluePumpkin</small> ```java= package com.fju; public class StarPyramid { public static void main(String[] args) { for (int i =0;i<10;i++){ for (int j=0;j<19;j++){ if (10 - i >= j||10+i<=j){ System.out.print(" "); }else{ System.out.print("*"); } } System.out.println(); } } } ``` ::: ###### tags:`程式設計` `Java` `110` `2021` <style> .navbar-brand::after { content: " × FJUMIIA"; } </style>