先上结果

Java程序会先把所有的静态模块提取出来优先执行

四则运算主程序代码

  1 import java.util.Scanner;
2
3
4 public class main {
5 public static void show() {
6 System.out.println("1.参数设置");
7 System.out.println("2.小学二年级口算题");
8 System.out.println("3.小学三年级口算题");
9 System.out.println("4.小学四年级口算题");
10 }
11
12 public static void showP() {
13 System.out.println("1.做新题");
14 System.out.println("2.做错题");
15 }
16
17 public static void main(String[] args) {
18 int nums = 30;//出题个数
19 int rangeMin = 0;//出题下限
20 int rangeMax = 10;//出题上限
21
22 Scanner sc = new Scanner(System.in);
23
24 while (true) {
25 show();
26 int choice;
27 choice = sc.nextInt();
28
29 switch (choice) {
30
31 case 1: {
32 System.out.println("请输入出题个数:");
33 nums = sc.nextInt();
34 System.out.println("请输入出题下限:");
35 rangeMin = sc.nextInt();
36 System.out.println("请输入出题上限:");
37 rangeMax = sc.nextInt();
38 break;
39 }
40
41 case 2: {
42 Second sd = new Second(nums, 2, rangeMin, rangeMax);
43 int flag = 1;
44 int i = 0;
45 while (i < nums && flag != 0) {
46 if (sd.judge() == 0) {
47 flag = 1;
48 break;
49 } else {
50 showP();
51 int choice2 = sc.nextInt();
52 if (choice2 == 1) {
53 sd.ff();
54 } else {
55 sd.f6();
56 }
57 i++;
58 System.out.println("按任一数字键继续,输入'0'回到主界面");
59 flag = sc.nextInt();
60 }
61 }
62 break;
63 }
64
65 case 3: {
66 Third td = new Third(nums, 4, rangeMin, rangeMax);
67 int flag = 1;
68 int i = 0;
69 while (i < nums && flag != 0) {
70 if (td.judge() == 0) {
71 flag = 1;
72 break;
73 } else {
74 showP();
75 int choice2 = sc.nextInt();
76 if (choice2 == 1) {
77 td.ff();
78 } else {
79 td.f6();
80 }
81 i++;
82 System.out.println("按任一数字键继续,输入'0'回到主界面");
83 flag = sc.nextInt();
84 }
85 }
86 break;
87 }
88
89 case 4: {
90 Forth ft = new Forth(nums, 4, rangeMin, rangeMax);
91 int flag = 1;
92 int i = 0;
93 while (i < nums && flag != 0) {
94 showP();
95 ft.judge();
96 int choice2 = sc.nextInt();
97 if (choice2 == 1) {
98 ft.ff();
99 } else {
100 ft.f6();
101 }
102 i++;
103 System.out.println("按任一数字键继续,输入'0'回到主界面");
104 flag = sc.nextInt();
105 }
106 break;
107 }
108 }
109 }
110 }
111 }

主要的父类Second类

  1 import java.util.Random;
2 import java.util.Scanner;
3
4 public class Second {
5 public int len1;//打印个数
6 public int len2;//操作数个数
7 public int multi = 1;//乘除法
8 public int paren = 0;//括号
9 public int leftR;//最小值
10 public int rightR;//最大值
11 public int leftP;//左括号
12 public int rightP;//右括号
13 public int index;//记录重复生成次数是否过多
14 double[] a;//存储操作数
15 int[] b;//存储操作符
16 String[] jiLu;//记录出现过的题目
17 double[] result;//记录答案
18 int[] fault;//记录错题下标
19 int id;
20 int cs = 2;
21
22 public int judge() {
23 if (this.rightR - this.leftR >= 100) {
24 System.out.println("操作数范围超过限制,请返回后重新配置正确");
25 return 0;
26 } else return 1;
27 }
28
29 public Second() {
30 }
31
32 public Second(int i1, int i2, int i5, int i6) {
33 this.len1 = i1;
34 this.len2 = i2;
35 this.leftR = i5;
36 this.rightR = i6;
37 a = new double[len2];//存储操作数
38 b = new int[len2 - 1];//存储操作符
39 jiLu = new String[len1];//记录出现过的题目
40 result = new double[len1];
41 fault = new int[len1 + 1];
42 id = 0;
43 }
44
45 public void ff() {
46 double index = 0.0;
47 if (this.multi == 1) {
48 this.multi = 4;
49 } else {
50 this.multi = 2;
51 }
52 Random random = new Random();
53
54 int i = 0;
55 if (this.paren == 1) {
56 this.paren = len1;
57 }
58
59 int temp = 1;
60 while (i < len1 && temp != 0) {
61 jiLu[i] = "";
62 String str = "";
63 for (int j = 0; j < len2 - 1; j++) {//生成操作符
64 b[j] = random.nextInt(this.multi);
65 jiLu[i] += b[j];
66 }
67
68 for (int j = 0; j < len2; j++) {//生成操作数
69 //System.out.println(j);
70 a[j] = random.nextInt(this.rightR - this.leftR + 1) + this.leftR;
71 if (j > 0) {
72 int tempR = 1;
73 double temP = 1;
74 if (this.cs == 2) {
75 tempR = (int) (a[j - 1] / a[j]);
76 temP = a[j] - temp;
77 if (temP - tempR != 0) {
78 b[j - 1] = random.nextInt(2);
79 }
80 }
81 while (b[j - 1] == 3 && a[j] == 0) {
82 //System.out.println("----");
83 a[j] = random.nextInt(this.rightR - this.leftR + 1) + this.leftR;
84 }
85 }
86 jiLu[i] += a[j];
87 }
88
89 if (this.paren != 0) {
90 //0+0+0=
91 this.leftP = random.nextInt(len2 - 1);//左括号能存在的位置
92 jiLu[i] += this.leftP;
93 this.rightP = random.nextInt(len2 - this.leftP - 1) + this.leftP + 1;//右括号位置
94 jiLu[i] += this.rightP;
95 }
96
97 int flag = f3(i);//计算是否重复
98 jiLu[i] = "";
99 if (flag == 0) {
100 int iFlag = 0;
101 for (int j = 0; j < len2; j++) {
102 if (this.paren >= 1 && this.leftP == j) {
103 System.out.print("(");
104 jiLu[i] += "(";
105 }
106 System.out.print((int) a[j]);
107 jiLu[i] += (int) a[j];
108 if (this.paren >= 1 && this.rightP == j) {
109 System.out.print(")");
110 jiLu[i] += ")";
111 this.paren--;
112 }
113 if (iFlag != len2 - 1) {
114 System.out.print(this.f2(b[j]));
115 jiLu[i] += this.f2(b[j]);
116 }
117 iFlag++;
118 }
119 System.out.print("=");
120 jiLu[i] += "=";
121 f4(i);
122 if (f5(i) == 0) {
123 this.fault[this.id] = i;
124 this.setId();
125 index++;
126 }
127 System.out.println("是否继续答题,按任一数字键继续,按0返回");
128 Scanner sc = new Scanner(System.in);
129 temp = sc.nextInt();
130 i++;
131 }
132 }
133 System.out.print("错题率为:");
134 //System.out.println(index);
135 //System.out.println(len1);
136 double len = i + 0.0;
137 double rate = index * 100 / len;
138 //System.out.println(rate);
139 System.out.println(String.format("%.2f", rate) + "%");
140 }
141
142 public void setId() {
143 this.id++;
144 }
145
146 public int f3(int i) {
147 if (this.index >= len1 * len1) {
148 i = len1;
149 System.out.println("重复生成次数过多,程序自动结束,请检查操作数个数与生成题目之间是否合法");
150 }
151
152 for (int j = 1; j < i; j++) {
153 if (jiLu[j].equals(jiLu[i])) {
154 this.index++;
155 return 1;
156 }
157 }
158 return 0;
159 }
160
161 public String f2(int i) {
162 if (i == 0) {
163 return "+";
164 } else if (i == 1) {
165 return "-";
166 } else if (i == 2) {
167 return "*";
168 } else {
169 return "/";
170 }
171 }
172
173 public double f4(int t) {
174 double dR = 0.0;
175 if (this.paren == 0) {//没有括号
176 for (int j = 0; j < len2 - 1; j++) {
177 if (this.b[j] == 1) {
178 a[j + 1] = -a[j + 1];
179 }
180 if (this.b[j] == 2) {
181 dR = a[j] * a[j + 1];
182 a[j] = 0;
183 a[j + 1] = dR;
184 }
185 if (this.b[j] == 3) {
186 dR = a[j] / a[j + 1];
187 a[j] = 0;
188 a[j + 1] = dR;
189 }
190 }
191
192 result[t] = 0.0;
193 for (int j = 0; j < len2; j++) {
194 result[t] += a[j];
195 }
196 } else {
197 //System.out.println("-----");
198 for (int i = this.leftP; i < this.rightP; i++) {//计算括号内
199 if (this.b[i] == 1) {
200 a[i + 1] = -a[i + 1];
201 }
202 if (this.b[i] == 2) {
203 dR = a[i] * a[i + 1];
204 a[i] = 0.0;
205 a[i + 1] = dR;
206 }
207 if (this.b[i] == 3) {
208 dR = a[i] / a[i + 1];
209 a[i] = 0;
210 a[i + 1] = dR;
211 }
212 }
213 dR = 0.0;
214 for (int j = this.leftP; j < this.rightP + 1; j++) {
215 dR += a[j];
216 }
217 a[this.leftP] = dR;//括号内计算结果
218
219 double[] temp = new double[len2 - (this.rightP - this.leftP)];//存去掉括号后的操作数
220 int[] tempB = new int[len2 - (this.rightP - this.leftP) - 1];//存括号外的操作符
221
222 temp[this.leftP] = dR;
223 if (this.rightP != len2 - 1) {
224 tempB[this.leftP] = b[this.rightP];
225 }
226
227 for (int i = 0; i < this.leftP; i++) {
228 temp[i] = a[i];
229 tempB[i] = b[i];
230 }
231 for (int i = this.rightP + 1, index = this.leftP + 1; i < len2; i++, index++) {
232 temp[index] = a[i];
233 }
234 for (int i = this.rightP + 1, index = this.leftP + 1; i < len2 - 1; i++, index++) {
235 tempB[index] = b[i];
236 }
237
238 for (int j = 0; j < len2 - (this.rightP - this.leftP) - 1; j++) {
239 if (tempB[j] == 1) {
240 temp[j + 1] = -temp[j + 1];
241 }
242 if (tempB[j] == 2) {
243 dR = temp[j] * temp[j + 1];
244 temp[j] = 0;
245 temp[j + 1] = dR;
246 }
247 if (tempB[j] == 3) {
248 dR = temp[j] / temp[j + 1];
249 temp[j] = 0;
250 temp[j + 1] = dR;
251 }
252 }
253
254 result[t] = 0.0;
255 for (int j = 0; j < len2 - (this.rightP - this.leftP); j++) {
256 result[t] += temp[j];
257 }
258 }
259 return result[t];
260 }
261
262 public int f5(int i) {
263 Scanner sc = new Scanner(System.in);
264 double d = sc.nextDouble();
265 if ((d - result[i] <= 0.01) && (d - result[i] >= -0.01)) {
266 System.out.println("回答正确");
267 return 1;
268 } else {
269 System.out.print("计算错误,正确答案为:");
270 System.out.println(String.format("%.2f", result[i]));
271 return 0;
272 }
273 }
274
275 public void f6() {
276 for (int i = 0; i < this.id; i++) {
277 if (this.fault[i] != -1) {
278 System.out.print(jiLu[this.fault[i]]);
279 if (f5(this.fault[i]) == 1) {
280 this.fault[i] = -1;
281 }
282 }
283 }
284 for (int i = 0; i < this.id; i++) {
285 if (this.fault[i] == -1) {
286 for (int j = i; j < this.id - 1; j++) {
287 this.fault[j] = this.fault[j + 1];
288 }
289 this.id--;
290 }
291 }
292 System.out.println("错题展示完毕");
293 }
294 }

对于三年级和四年级的主要部分我直接继承使用二年级类

Third类增多的部分

 1 public int judge() {
2 if (this.rightR - this.leftR >= 1000) {
3 System.out.println("操作数范围超过限制,请返回后重新配置正确");
4 return 0;
5 } else return 1;
6 }
7
8 public Third(int i1, int i2, int i5, int i6) {
9 this.cs=3;
10 this.len1 = i1;
11 this.len2 = i2;
12 this.leftR = i5;
13 this.rightR = i6;
14 a = new double[len2];//存储操作数
15 b = new int[len2 - 1];//存储操作符
16 jiLu = new String[len1];//记录出现过的题目
17 result = new double[len1];
18 fault = new int[len1 + 1];
19 id = 0;
20 }

Forth不同的部分

 1   public int judge() {
2 this.paren = 1;
3 return 1;
4 }
5
6 public Forth(int i1, int i2, int i5, int i6) {
7 this.cs = 4;
8 this.len1 = i1;
9 this.len2 = i2;
10 this.leftR = i5;
11 this.rightR = i6;
12 a = new double[len2];//存储操作数
13 b = new int[len2 - 1];//存储操作符
14 jiLu = new String[len1];//记录出现过的题目
15 result = new double[len1];
16 fault = new int[len1 + 1];
17 id = 0;
18 }

因为上一次写的代码都在一个类内,并且功能已经足够丰富,能覆盖当前的要求,所以我想着把该类变为基类,同时因为一些条件限制,我选择直接在Second类直接给某些元素赋初值,进而实现功能削减同时又不影响Third和Forth类功能。

另外一个变更的点是错题率的计算,因为每做一题就询问是否继续,以当前所做完题目数为基准更具有说明性

动手动脑3&课堂作业(四则运算与继承)的更多相关文章

  1. JAVA动手动脑及课后作业

    1.查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 运行结果 true true false 原因 1)在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0, ...

  2. Java动手动脑第四讲课堂作业

    动手动脑1 完全“手写代码实现”随机数生成 纯随机数发生器

  3. java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )

    一.课堂测试 1.课堂测试:花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求 (1)减法结果不能为负数 (2)乘法结果不得超过一百,除法结果必须为整数 (3)题目避免重复: (4)可 ...

  4. java课堂动手动脑及课后实验总结

      动手动脑一:枚举   输出结果: false false true SMALL MEDIUM LARGE 分析和总结用法 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本 ...

  5. Java(接口与继承)动手动脑

    1>继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改 Parent 构造方法的代码,显式调用 GrandParen ...

  6. JAVA 数组作业——动手动脑以及课后实验性问题

    JAVA课后作业——动手动脑 一:阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 1.源代码 // PassArray.java // ...

  7. Java动手动脑——多态和继承

    Java动手动脑——继承和多态 实验一 预估输出答案:100  200  201  202 输出结果:100  200  201  202 输出答案分析:100 创建parent类的对象,调用对象的方 ...

  8. java语言课堂动手动脑

    1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

  9. JAVA语法基础作业——动手动脑以及课后实验性问题 (八)

    一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识. 1)源代码 import javax.swing.*; class AboutException ...

随机推荐

  1. *****又错一道,不知道为啥。。。 ybt【例5.19】字符串判等

    [题目描述] 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. [输入] 两行,每行包含一个字符串. [输出] 若两个字符串相等,输出YES,否则输出NO. 代码我觉得没啥问 ...

  2. python3生成一个含有20个随机数的列表,要求所有元素不相同,并且每个元素的值介于1到100之间

    import random alist = random.sample(range(1,101),20) #random.sample()生成不相同的随机数 print(alist)

  3. kubernetes关于证书配置得问题总结

    总结证书配置 1.证书首先分为两种配置方式, 1) 一种是在集群中配置 2) 一种是在上游负载均衡中配置. 1)https证书在集群中配置,并域名直接解析到集群的ingress-nginx-contr ...

  4. collections 数据类型扩展模块

    在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtuple和Ord ...

  5. @Autowired 注解?

    @Autowired 注解提供了更细粒度的控制,包括在何处以及如何完成自动装配.它的用法和@Required一样,修饰setter方法.构造器.属性或者具有任意名称和/或多个参数的PN方法.

  6. Kafka新建的分区会在哪个目录下创建?

    在启动 Kafka 集群之前,我们需要配置好 log.dirs 参数,其值是 Kafka 数据的存放目录,这个参数可以配置多个目录,目录之间使用逗号分隔,通常这些目录是分布在不同的磁盘上用于提高读写性 ...

  7. brew 安装redis

    转:https://www.jianshu.com/p/e1e5717049e8 编辑新安装php的 p.p1 { margin: 0; font: 11px Menlo; color: rgba(0 ...

  8. CHAR 和 VARCHAR 的区别?

    1.CHAR 和 VARCHAR 类型在存储和检索方面有所不同 2.CHAR 列长度固定为创建表时声明的长度,长度值范围是 1 到 255 当 CHAR 值被存储时,它们被用空格填充到特定长度,检索  ...

  9. 初识Spring(为什么要使用Spring?)

    Spring,英文翻译是春天的意思,而在Java中,是一个开放源代码的设计层面框架(手动滑稽,程序员的春天),他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.S ...

  10. Spring 切面可以应用五种类型的通知?

    Spring 切面可以应用五种类型的通知: before:前置通知,在一个方法执行前被调用. after: 在方法执行之后调用的通知,无论方法执行是否成功. after-returning: 仅当方法 ...