Java学习十七

今天对自己之前的四则运算进行了再次优化。

这次的优化有三个类,Grade2,Grade3,Grade4这三个类是多重继承关系,以便提高代码的复用性,简化了main()方法。

下面是我的源码

 1 import java.util.Scanner;
 2 
 3 //二柱子四则运算
 4 public class RanE {
 5     static Scanner sc=new Scanner(System.in);
 6     public static void main(String[] args) {
 7         System.out.println("请选择:1、二年级口算题;2、三年级口算题;3、四年级口算题;4、退出");
 8         int i=sc.nextInt();
 9         switch(i) {
10         case 1:Grade2 s1=new Grade2();
11             s1.answer();
12             break;
13         case 2:
14             Grade3 s2=new Grade3();
15             s2.select();
16             break;
17         case 3:Grade4 s3=new Grade4();
18             s3.select1();
19             break;
20         case 4:System.out.println("谢谢使用!");
21             System.exit(0);
22         }
23     }
24 }
 1 import java.util.ArrayList;
 2 import java.util.Random;
 3 import java.util.Scanner;
 4 
 5 public class Grade2 {
 6     static ArrayList<String> s=new ArrayList<String>();
 7     static int[] m=new int[100];
 8     static int[] n=new int[100];
 9     static int i;
10     static Random r=new Random();
11     static Scanner sc=new Scanner(System.in);
12     static int num;
13     public static void answer() {
14         double count=0;
15         System.out.print("请输入范围(<=100):");
16         num=sc.nextInt();
17         System.out.print("请输入题目数(>30):");
18         int number=sc.nextInt();
19         for(i=0;i<2*number;i++) {
20             m[i]=r.nextInt(num)+1;
21             n[i]=r.nextInt(num)+1;
22             for(int j=0;j<i;j++) {
23                 if(m[i]==m[j]&&n[i]==n[j]) {
24                     m[i]=r.nextInt(num)+1;
25                 }
26             }
27             int k=r.nextInt(4)+1;
28             switch(k) {
29             case 0:System.out.println(m[i]+"+"+n[i]+"=");
30                     double a=sc.nextInt(); 
31                     System.out.println("正确答案:"+(m[i]+n[i]));
32                     if(a!=(m[i]+n[i])) {
33                         String e=m[i]+"+"+n[i]+"=";
34                         s.add(e);
35                         count++;
36                     }
37                     break;
38             case 1:System.out.println(m[i]+"-"+n[i]+"=");
39             double b=sc.nextInt();
40             System.out.println("正确答案:"+(m[i]-n[i]));
41             if(b!=(m[i]-n[i])) {
42                 String e=m[i]+"-"+n[i]+"=";
43                 s.add(e);
44                 count++;
45             }
46             break;
47             case 2:System.out.println(m[i]+"*"+n[i]+"=");
48             double c=sc.nextInt();
49             System.out.println("正确答案:"+(m[i]*n[i]));
50             if(c!=(m[i]*n[i])) {
51                 String e=m[i]+"*"+n[i]+"=";
52                 s.add(e);
53                 count++;
54             }
55             break;
56             case 3:System.out.println(m[i]+"/"+n[i]+"=");
57             double d=sc.nextInt();
58             System.out.println("正确答案:"+(m[i]/n[i]));
59             if(d!=(m[i]/n[i])) {
60                 String e=m[i]+"/"+n[i]+"=";
61                 s.add(e);
62                 count++;
63             }
64             break;
65             }
66         }
67         System.out.println("正确率:"+(100-(count/number)*100)+"%");
68         System.out.println("错题数目:"+count);
69         System.out.println("错题:"+s);
70     }
71 
72 
73 }
  1 import java.util.Scanner;
  2 public class Grade3 extends Grade2{
  3     static int[] j=new int[100];
  4     static int[] z=new int[100];
  5     static Scanner sc=new Scanner(System.in);
  6     public static void select() {
  7         System.out.println("请选择操作数:1:2个;2:3个;3:4个");
  8         int i=sc.nextInt();
  9         switch(i) {
 10         case 1:answer1();
 11             break;
 12         case 2:answer2();
 13             break;
 14         case 3:answer3();
 15             break;
 16         }
 17     }
 18     
 19     public static void answer1() {
 20         double count=0;
 21         System.out.print("请输入范围(<=100):");
 22         num=sc.nextInt();
 23         System.out.print("请输入题目数(>30):");
 24         int number=sc.nextInt();
 25         for(i=0;i<2*number;i++) {
 26             m[i]=r.nextInt(num)+1;
 27             n[i]=r.nextInt(num)+1;
 28             for(int j=0;j<i;j++) {
 29                 if(m[i]==m[j]&&n[i]==n[j]) {
 30                     m[i]=r.nextInt(num)+1;
 31                 }
 32             }
 33             int k=r.nextInt(4)+1;
 34             switch(k) {
 35             case 0:System.out.println(m[i]+"+"+n[i]+"=");
 36                     double a=sc.nextInt(); 
 37                     System.out.println("正确答案:"+(m[i]+n[i]));
 38                     if(a!=(m[i]+n[i])) {
 39                         String e=m[i]+"+"+n[i]+"=";
 40                         s.add(e);
 41                         count++;
 42                     }
 43                     break;
 44             case 1:System.out.println(m[i]+"-"+n[i]+"=");
 45             double b=sc.nextInt();
 46             System.out.println("正确答案:"+(m[i]-n[i]));
 47             if(b!=(m[i]-n[i])) {
 48                 String e=m[i]+"-"+n[i]+"=";
 49                 s.add(e);
 50                 count++;
 51             }
 52             break;
 53             case 2:System.out.println(m[i]+"*"+n[i]+"=");
 54             double c=sc.nextInt();
 55             System.out.println("正确答案:"+(m[i]*n[i]));
 56             if(c!=(m[i]*n[i])) {
 57                 String e=m[i]+"*"+n[i]+"=";
 58                 s.add(e);
 59                 count++;
 60             }
 61             break;
 62             case 3:System.out.println(m[i]+"/"+n[i]+"=");
 63             double d=sc.nextInt();
 64             System.out.println("正确答案:"+(m[i]/n[i]));
 65             if(d!=(m[i]/n[i])) {
 66                 String e=m[i]+"/"+n[i]+"=";
 67                 s.add(e);
 68                 count++;
 69             }
 70             break;
 71             }
 72         }
 73         System.out.println("正确率:"+(100-(count/number)*100)+"%");
 74         System.out.println("错题数目:"+count);
 75         System.out.println("错题:"+s);
 76     }
 77 
 78     
 79     public static void answer2() {
 80         double count=0;
 81         System.out.print("请输入范围(<=1000):");
 82         num=sc.nextInt();
 83         System.out.print("请输入题目数(>30):");
 84         int number=sc.nextInt();
 85         for(i=0;i<2*number;i++) {
 86             m[i]=r.nextInt(num)+1;
 87             n[i]=r.nextInt(num)+1;
 88             j[i]=r.nextInt(num)+1;
 89             for(int j=0;j<i;j++) {
 90                 if(m[i]==m[j]&&n[i]==n[j]) {
 91                     m[i]=r.nextInt(num)+1;
 92                 }
 93             }
 94             int k=r.nextInt(4)+1;
 95             switch(k) {
 96             case 0:System.out.println(m[i]+"+"+n[i]+"-"+j[i]+"=");
 97                     double a=sc.nextInt(); 
 98                     System.out.println("正确答案:"+(m[i]+n[i]-j[i]));
 99                     if(a!=(m[i]+n[i])) {
100                         String e=m[i]+"+"+n[i]+"-"+j[i]+"=";
101                         s.add(e);
102                         count++;
103                     }
104                     break;
105             case 1:System.out.println(j[i]+"*"+m[i]+"-"+n[i]+"=");
106             double b=sc.nextInt();
107             System.out.println("正确答案:"+(j[i]*m[i]-n[i]));
108             if(b!=(m[i]-n[i])) {
109                 String e=j[i]+"*"+m[i]+"-"+n[i]+"=";
110                 s.add(e);
111                 count++;
112             }
113             break;
114             case 2:System.out.println(m[i]+"*"+n[i]+"/"+j[i]+"=");
115             double c=sc.nextInt();
116             System.out.println("正确答案:"+(m[i]*n[i]/j[i]));
117             if(c!=(m[i]*n[i])) {
118                 String e=m[i]+"*"+n[i]+"/"+j[i]+"=";
119                 s.add(e);
120                 count++;
121             }
122             break;
123             case 3:System.out.println(m[i]+"/"+n[i]+"+"+j[i]+"=");
124             double d=sc.nextInt();
125             System.out.println("正确答案:"+(m[i]/n[i]+j[i]));
126             if(d!=(m[i]/n[i])) {
127                 String e=m[i]+"/"+n[i]+"+"+j[i]+"=";
128                 s.add(e);
129                 count++;
130             }
131             break;
132             }
133         }
134         System.out.println("正确率:"+(100-(count/number)*100)+"%");
135         System.out.println("错题数目:"+count);
136         System.out.println("错题:"+s);
137     }
138 
139     public static void answer3() {
140         double count=0;
141         System.out.print("请输入范围(<=1000):");
142         num=sc.nextInt();
143         System.out.print("请输入题目数(>30):");
144         int number=sc.nextInt();
145         for(i=0;i<2*number;i++) {
146             m[i]=r.nextInt(num)+1;
147             n[i]=r.nextInt(num)+1;
148             j[i]=r.nextInt(num)+1;
149             z[i]=r.nextInt(num)+1;
150             for(int j=0;j<i;j++) {
151                 if(m[i]==m[j]&&n[i]==n[j]) {
152                     m[i]=r.nextInt(num)+1;
153                 }
154             }
155             int k=r.nextInt(4)+1;
156             switch(k) {
157             case 0:System.out.println(z[i]+"*"+m[i]+"+"+n[i]+"-"+j[i]+"=");
158                     double a=sc.nextInt(); 
159                     System.out.println("正确答案:"+(z[i]*m[i]+n[i]-j[i]));
160                     if(a!=(m[i]+n[i])) {
161                         String e=z[i]+"*"+m[i]+"+"+n[i]+"-"+j[i]+"=";
162                         s.add(e);
163                         count++;
164                     }
165                     break;
166             case 1:System.out.println(m[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=");
167             double b=sc.nextInt();
168             System.out.println("正确答案:"+(m[i]/j[i]+z[i]-n[i]));
169             if(b!=(m[i]-n[i])) {
170                 String e=m[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=";
171                 s.add(e);
172                 count++;
173             }
174             break;
175             case 2:System.out.println(z[i]+"-"+m[i]+"*"+n[i]+"/"+j[i]+"=");
176             double c=sc.nextInt();
177             System.out.println("正确答案:"+(z[i]-m[i]*n[i]/j[i]));
178             if(c!=(m[i]*n[i])) {
179                 String e=z[i]+"-"+m[i]+"*"+n[i]+"/"+j[i]+"=";
180                 s.add(e);
181                 count++;
182             }
183             break;
184             case 3:System.out.println(j[i]+"-"+m[i]+"/"+"-"+z[i]+n[i]+"=");
185             double d=sc.nextInt();
186             System.out.println("正确答案:"+(m[i]/n[i]));
187             if(d!=(m[i]/n[i])) {
188                 String e=m[i]+"/"+n[i]+"=";
189                 s.add(e);
190                 count++;
191             }
192             break;
193             }
194         }
195         System.out.println("正确率:"+(100-(count/number)*100)+"%");
196         System.out.println("错题数目:"+count);
197         System.out.println("错题:"+s);
198     }
199 
200     public Grade3() {
201         super();
202     }
203 }
  1 import java.util.Scanner;
  2 public class Grade4 extends Grade3{
  3     static Scanner sc=new Scanner(System.in);
  4     static int[] q=new int[100];
  5     public static void select1() {
  6         System.out.println("请选择操作数:1:2个;2:3个;3:4个;4:5个(含括号)");
  7         int i=sc.nextInt();
  8         switch(i) {
  9         case 1:answer1();
 10             break;
 11         case 2:answer2();
 12             break;
 13         case 3:answer3();
 14             break;
 15         case 4:answer4();
 16             break;
 17         }
 18     }
 19     
 20     public static void answer1() {
 21         double count=0;
 22         System.out.print("请输入范围(<=100):");
 23         num=sc.nextInt();
 24         System.out.print("请输入题目数(>30):");
 25         int number=sc.nextInt();
 26         for(i=0;i<2*number;i++) {
 27             m[i]=r.nextInt(num)+1;
 28             n[i]=r.nextInt(num)+1;
 29             for(int j=0;j<i;j++) {
 30                 if(m[i]==m[j]&&n[i]==n[j]) {
 31                     m[i]=r.nextInt(num)+1;
 32                 }
 33             }
 34             int k=r.nextInt(4)+1;
 35             switch(k) {
 36             case 0:System.out.println(m[i]+"+"+n[i]+"=");
 37                     double a=sc.nextInt(); 
 38                     System.out.println("正确答案:"+(m[i]+n[i]));
 39                     if(a!=(m[i]+n[i])) {
 40                         String e=m[i]+"+"+n[i]+"=";
 41                         s.add(e);
 42                         count++;
 43                     }
 44                     break;
 45             case 1:System.out.println(m[i]+"-"+n[i]+"=");
 46             double b=sc.nextInt();
 47             System.out.println("正确答案:"+(m[i]-n[i]));
 48             if(b!=(m[i]-n[i])) {
 49                 String e=m[i]+"-"+n[i]+"=";
 50                 s.add(e);
 51                 count++;
 52             }
 53             break;
 54             case 2:System.out.println(m[i]+"*"+n[i]+"=");
 55             double c=sc.nextInt();
 56             System.out.println("正确答案:"+(m[i]*n[i]));
 57             if(c!=(m[i]*n[i])) {
 58                 String e=m[i]+"*"+n[i]+"=";
 59                 s.add(e);
 60                 count++;
 61             }
 62             break;
 63             case 3:System.out.println(m[i]+"/"+n[i]+"=");
 64             double d=sc.nextInt();
 65             System.out.println("正确答案:"+(m[i]/n[i]));
 66             if(d!=(m[i]/n[i])) {
 67                 String e=m[i]+"/"+n[i]+"=";
 68                 s.add(e);
 69                 count++;
 70             }
 71             break;
 72             }
 73         }
 74         System.out.println("正确率:"+(100-(count/number)*100)+"%");
 75         System.out.println("错题数目:"+count);
 76         System.out.println("错题:"+s);
 77     }
 78 
 79     
 80     public static void answer2() {
 81         double count=0;
 82         System.out.print("请输入范围(<=1000):");
 83         num=sc.nextInt();
 84         System.out.print("请输入题目数(>30):");
 85         int number=sc.nextInt();
 86         for(i=0;i<2*number;i++) {
 87             m[i]=r.nextInt(num)+1;
 88             n[i]=r.nextInt(num)+1;
 89             j[i]=r.nextInt(num)+1;
 90             for(int j=0;j<i;j++) {
 91                 if(m[i]==m[j]&&n[i]==n[j]) {
 92                     m[i]=r.nextInt(num)+1;
 93                 }
 94             }
 95             int k=r.nextInt(4)+1;
 96             switch(k) {
 97             case 0:System.out.println(m[i]+"+"+n[i]+"-"+j[i]+"=");
 98                     double a=sc.nextInt(); 
 99                     System.out.println("正确答案:"+(m[i]+n[i]-j[i]));
100                     if(a!=(m[i]+n[i])) {
101                         String e=m[i]+"+"+n[i]+"-"+j[i]+"=";
102                         s.add(e);
103                         count++;
104                     }
105                     break;
106             case 1:System.out.println(j[i]+"*"+m[i]+"-"+n[i]+"=");
107             double b=sc.nextInt();
108             System.out.println("正确答案:"+(j[i]*m[i]-n[i]));
109             if(b!=(m[i]-n[i])) {
110                 String e=j[i]+"*"+m[i]+"-"+n[i]+"=";
111                 s.add(e);
112                 count++;
113             }
114             break;
115             case 2:System.out.println(m[i]+"*"+n[i]+"/"+j[i]+"=");
116             double c=sc.nextInt();
117             System.out.println("正确答案:"+(m[i]*n[i]/j[i]));
118             if(c!=(m[i]*n[i])) {
119                 String e=m[i]+"*"+n[i]+"/"+j[i]+"=";
120                 s.add(e);
121                 count++;
122             }
123             break;
124             case 3:System.out.println(m[i]+"/"+n[i]+"+"+j[i]+"=");
125             double d=sc.nextInt();
126             System.out.println("正确答案:"+(m[i]/n[i]+j[i]));
127             if(d!=(m[i]/n[i])) {
128                 String e=m[i]+"/"+n[i]+"+"+j[i]+"=";
129                 s.add(e);
130                 count++;
131             }
132             break;
133             }
134         }
135         System.out.println("正确率:"+(100-(count/number)*100)+"%");
136         System.out.println("错题数目:"+count);
137         System.out.println("错题:"+s);
138     }
139 
140     public static void answer3() {
141         double count=0;
142         System.out.print("请输入范围(<=1000):");
143         num=sc.nextInt();
144         System.out.print("请输入题目数(>30):");
145         int number=sc.nextInt();
146         for(i=0;i<2*number;i++) {
147             m[i]=r.nextInt(num)+1;
148             n[i]=r.nextInt(num)+1;
149             j[i]=r.nextInt(num)+1;
150             z[i]=r.nextInt(num)+1;
151             for(int j=0;j<i;j++) {
152                 if(m[i]==m[j]&&n[i]==n[j]) {
153                     m[i]=r.nextInt(num)+1;
154                 }
155             }
156             int k=r.nextInt(4)+1;
157             switch(k) {
158             case 0:System.out.println(z[i]+"*"+m[i]+"+"+n[i]+"-"+j[i]+"=");
159                     double a=sc.nextInt(); 
160                     System.out.println("正确答案:"+(z[i]*m[i]+n[i]-j[i]));
161                     if(a!=(m[i]+n[i])) {
162                         String e=z[i]+"*"+m[i]+"+"+n[i]+"-"+j[i]+"=";
163                         s.add(e);
164                         count++;
165                     }
166                     break;
167             case 1:System.out.println(m[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=");
168             double b=sc.nextInt();
169             System.out.println("正确答案:"+(m[i]/j[i]+z[i]-n[i]));
170             if(b!=(m[i]-n[i])) {
171                 String e=m[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=";
172                 s.add(e);
173                 count++;
174             }
175             break;
176             case 2:System.out.println(z[i]+"-"+m[i]+"*"+n[i]+"/"+j[i]+"=");
177             double c=sc.nextInt();
178             System.out.println("正确答案:"+(z[i]-m[i]*n[i]/j[i]));
179             if(c!=(m[i]*n[i])) {
180                 String e=z[i]+"-"+m[i]+"*"+n[i]+"/"+j[i]+"=";
181                 s.add(e);
182                 count++;
183             }
184             break;
185             case 3:System.out.println(j[i]+"-"+m[i]+"/"+z[i]+"-"+n[i]+"=");
186             double d=sc.nextInt();
187             System.out.println("正确答案:"+(j[i]-m[i]/z[i]-n[i]));
188             if(d!=(m[i]/n[i])) {
189                 String e=j[i]+"-"+m[i]+"/"+z[i]+"-"+n[i]+"=";
190                 s.add(e);
191                 count++;
192             }
193             break;
194             }
195         }
196         System.out.println("正确率:"+(100-(count/number)*100)+"%");
197         System.out.println("错题数目:"+count);
198         System.out.println("错题:"+s);
199     }
200 
201     public static void answer4() {
202         double count=0;
203         System.out.print("请输入范围(<=1000):");
204         num=sc.nextInt();
205         System.out.print("请输入题目数(>30):");
206         int number=sc.nextInt();
207         for(i=0;i<2*number;i++) {
208             m[i]=r.nextInt(num)+1;
209             n[i]=r.nextInt(num)+1;
210             j[i]=r.nextInt(num)+1;
211             z[i]=r.nextInt(num)+1;
212             q[i]=r.nextInt(num)+1;
213             for(int j=0;j<i;j++) {
214                 if(m[i]==m[j]&&n[i]==n[j]) {
215                     m[i]=r.nextInt(num)+1;
216                 }
217             }
218             int k=r.nextInt(4)+1;
219             switch(k) {
220             case 0:System.out.println(z[i]+"-"+q[i]+"*"+"("+m[i]+"+"+n[i]+")"+"-"+j[i]+"=");
221                     double a=sc.nextInt(); 
222                     System.out.println("正确答案:"+(z[i]-q[i]*(m[i]+n[i])-j[i]));
223                     if(a!=(m[i]+n[i])) {
224                         String e=z[i]+"-"+q[i]+"*"+"("+m[i]+"+"+n[i]+")"+"-"+j[i]+"=";
225                         s.add(e);
226                         count++;
227                     }
228                     break;
229             case 1:System.out.println(m[i]+"-"+q[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=");
230             double b=sc.nextInt();
231             System.out.println("正确答案:"+(m[i]-q[i]/j[i]+z[i]-n[i]));
232             if(b!=(m[i]-n[i])) {
233                 String e=m[i]+"-"+q[i]+"/"+j[i]+"+"+z[i]+"-"+n[i]+"=";
234                 s.add(e);
235                 count++;
236             }
237             break;
238             case 2:System.out.println((z[i]+"-"+m[i]+"*"+q[i])+"*"+n[i]+"/"+j[i]+"=");
239             double c=sc.nextInt();
240             System.out.println("正确答案:"+((z[i]-m[i]*q[i])*n[i]/j[i]));
241             if(c!=(m[i]*n[i])) {
242                 String e=(z[i]+"-"+m[i]+"*"+q[i])+"*"+n[i]+"/"+j[i]+"=";
243                 s.add(e);
244                 count++;
245             }
246             break;
247             case 3:System.out.println(j[i]+"-"+m[i]+"/"+(z[i]+"-"+n[i])+"/"+q[i]+"=");
248             double d=sc.nextInt();
249             System.out.println("正确答案:"+(j[i]-m[i]/(z[i]-n[i])/q[i]));
250             if(d!=(m[i]/n[i])) {
251                 String e=j[i]+"-"+m[i]+"/"+(z[i]+"-"+n[i])+"/"+q[i]+"=";
252                 s.add(e);
253                 count++;
254             }
255             break;
256             }
257         }
258         System.out.println("正确率:"+(100-(count/number)*100)+"%");
259         System.out.println("错题数目:"+count);
260         System.out.println("错题:"+s);
261     }
262 
263 }

Grade3和Grade4要求操作较多,代码较长。

posted on 2021-10-08 19:11  跨越&尘世  阅读(60)  评论(0编辑  收藏  举报