骰子的妙用---课堂答题
生活随笔
收集整理的這篇文章主要介紹了
骰子的妙用---课堂答题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過兩次的作業,我對類有了更深的理解,進一步的理解了類之間的依賴和繼承關系,這次的作業我是現在作業本上畫好類圖,然后根據自己的需求來建立自己的代碼體系,我發現這樣能夠起到事半功倍的作用。
主要的類有:Test()主類,調用GiftPunish()獎勵懲罰和Qestion()提問類
?
類圖如下:
?
?
?
代碼如下:?
1 package dicehomework2; 2 3 public class Test { 4 5 6 public static void main(String[] args) { 7 Question qe =new Question(); 8 GiftPunish gp=new GiftPunish(); 9 10 //設定班級學生數,通過學號選取作答的學生 11 qe.setNum(3); 12 qe.roll(); 13 qe.getStuid(); 14 qe.setOutPut(); 15 16 //顯示問題,并提交答案 17 qe.setQuestion(); 18 qe.anwserquestion(); 19 20 21 //由輸入的答案判斷是否正確,并進行獎懲 22 int inpu=qe.getInp(); 23 String pta=qe.getPt(); 24 switch (inpu){ 25 case 1: 26 if(pta.equals("武漢")) { 27 System.out.println("恭喜你,答對了,系統正在給你分配獎品!"); 28 gp.gift(); 29 } 30 else { 31 System.out.println("抱歉,請接受懲罰!"); 32 gp.punish(); 33 } 34 break; 35 case 2: 36 if(pta.equals("渝")) { 37 System.out.println("恭喜你,答對了!"); 38 gp.gift(); 39 } 40 else { 41 System.out.println("抱歉,請接受懲罰!"); 42 gp.punish(); 43 } 44 break; 45 default: 46 System.out.println("error!"); 47 } 48 } 49 50 } Test 1 package dicehomework2; 2 3 public class GiftPunish { 4 Question q=new Question(); 5 private int shuzi; 6 public void gift() { 7 q.setNum(3); 8 q.roll(); 9 shuzi=q.getStuid(); 10 11 switch(shuzi) { 12 case 1: 13 System.out.println("congraduation!!you get a notebook"); 14 break; 15 case 2: 16 System.out.println("congraduation!!you get a pen"); 17 break; 18 case 3: 19 System.out.println("congraduation!!you get a notebook"); 20 default: 21 System.out.println("sorry,please try again!!"); 22 break; 23 } 24 } 25 26 public void punish() { 27 q.setNum(2); 28 q.roll(); 29 shuzi=q.getStuid(); 30 31 switch(shuzi) { 32 case 1: 33 System.out.println("come on,do 30 push-up!!"); 34 break; 35 case 2: 36 System.out.println("oh,please run 100 laps around the playground!!!"); 37 break; 38 default: 39 System.out.println("sorry,please try again!!"); 40 break; 41 } 42 } 43 44 45 } PunishGift 1 package dicehomework2; 2 import java.util.Scanner; 3 4 public class Question { 5 private int stuid; 6 private int num; 7 String pt; 8 int inp; 9 10 11 12 13 public void roll() { 14 stuid=(int)(Math.random()*num+1); 15 } 16 17 public void setNum(int num) { 18 this.num=num; 19 } 20 21 public int getStuid() { 22 return stuid; 23 } 24 25 public void setOutPut() { 26 System.out.println("你選中的學生的學號是:"+stuid); 27 } 28 29 public void setQuestion() { 30 System.out.println("1、江城是什么的簡稱?"+"\n"+"2、重慶簡稱什么?"); 31 } 32 33 public void anwserquestion() { 34 System.out.println("please input the work number!!"); 35 Scanner input=new Scanner(System.in); 36 inp=input.nextInt(); 37 38 switch (inp){ 39 case 1: 40 System.out.println("your choose is question 1"+"\n"); 41 break; 42 case 2: 43 System.out.println("your choose is question 2"+"\n"); 44 break; 45 default: 46 System.out.println("請輸入1或2!"); 47 break; 48 } 49 50 System.out.println("please input the right answer!!"); 51 Scanner nput=new Scanner(System.in); 52 pt=nput.nextLine(); 53 } 54 55 56 //返回inp和pt值 57 public int getInp() { 58 return inp; 59 } 60 61 public String getPt() { 62 return pt; 63 } 64 } Question?實現的圖片如下:
小結:
這次的作業完成比上一次好多了,全部的內容都是自己獨立完成的,花了不少時間,開始找到了寫代碼的感覺,但是發現自己的基礎不是特別牢靠,比如因為不懂方法之間怎么調用的,然后控制臺提示出錯,后面請教同學才弄懂的;還有就是建GUI對我來說比較難,也是我花時間最多的一部分,現在只建成了一大半,我會多花點時間,在下次課前把它建好。
?
轉載于:https://www.cnblogs.com/jasonjan/p/7717447.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的骰子的妙用---课堂答题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建 Java 第三方类库,以及制作类库
- 下一篇: Tuple解决在视图中通过razor获取