骰子的妙用---课堂答题

通过两次的作业,我对类有了更深的理解,进一步的理解了类之间的依赖和继承关系,这次的作业我是现在作业本上画好类图,然后根据自己的需求来建立自己的代码体系,我发现这样能够起到事半功倍的作用。

主要的类有: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对我来说比较难,也是我花时间最多的一部分,现在只建成了一大半,我会多花点时间,在下次课前把它建好。

 

posted @ 2017-10-23 16:38  黄壘哦  阅读(629)  评论(0编辑  收藏  举报