1/*完成了计算,用户输入(请老师正常地输入)
* 缺点:校检输入还没有完成。
*还有就是代码比较乱,觉得有些代码可以写成方法的形式(还未整理),日后会进一步改善。
*/
2 package 软件工程;
3 import java.text.DecimalFormat;
4 /*
5 * 1.定制数量
6 * 2.控制是否有乘除法
7 * 3.控制数值范围
8 * 4.真分数练习题
9 */
10 import java.util.Scanner;
11
12 public class Main {
13 private static int calcMaxSubmultiple(int num1, int num2) {// 求最大公约数
14 int min = Math.min(num1, num2);
15 int maxSubmultiple = 1;
16 for (int i = min; i >= 1; i--) {
17 if (num1 % i == 0 && num2 % i == 0) {
18 maxSubmultiple = i;
19 }
20 break;
21 }
22 return maxSubmultiple;
23 }
24
25 public static void main(String[] args) {
26 int max = 10;// 控制算式个数
27 char[] op = { ' ', '+', '-', '*', '/' };// 操作符
28 int[] no = new int[4];// 操作符地址
29 int useno = 0;// 控制操作符
30 int n = 3;// 操作数个数(随机)
31 int[] num1 = new int[10];// 操作数
32 char opp;// 判断是否需要乘除法
33 char real;// 判断是否需要真分数的题目
34 int[] cs = { 1, 100 };// 数值范围
35 String[] useranser = new String[max];// 用户输入的答案
36 String[] staticanser = new String[max];// 标准答案
37 int sign; // 累加运算时的符号
38 float left, right;// 保存蹭结果
39 int f = 0;// 控制输出真分数的操作符
40 int count = 0;// 统计答题正确的数量
41 DecimalFormat decimal = new DecimalFormat("#.##");
42
43 int s1 = 1;// 分子通分
44 int ss1 = 1;// 分子通分
45 int s2 = 1;// 分母通分
46 int result = 0;// 分子计算
47 int gys;// 最大公约数
48 int ff = 0;// 分数除法,分子为0标志位
49 String zjfz = new String();// 最简分子
50 String zjfm = new String();// 最简分母
51
52
53 Scanner in = new Scanner(System.in);
54 System.out.print("请输入需定制的算式数量:");// 1.定制数量
55 max = in.nextInt();
56
57 System.out.print("是否需要乘除法(Y/N):");// 2.控制乘除参数
58 opp = in.next().charAt(0);
59 if (opp == 'Y' || opp == 'y') {
60 useno = 4;
61 } else if (opp == 'N' || opp == 'n') {
62 useno = 2;
63 }
64
65 System.out.print("参数范围(eg:1,100):");// 3.控制数值范围
66 String str = new String();
67 in.nextLine();// 过滤掉上面.next()方面的回车。
68 str = in.nextLine();
69 String[] ss = new String[2];
70 ss = str.split(",");
71 cs[0] = Integer.valueOf(ss[0]);
72 cs[1] = Integer.valueOf(ss[1]);
73
74 System.out.print("是否增加真分数练习题(Y/N):");// 3.控制数值范围
75 real = in.next().charAt(0);
76
77 System.out.println();
78 System.out.println(" 2014-2015学年度第一单元测试卷");
79 System.out
80 .println("班级: 姓名: 座号: 得分: ");
81 System.out.println();
82 System.out.println("一、请认真仔细地计算下面各题。(小数请保留小数点后两位)");
83 System.out.println();
84 for (int i = 0; i < max; i++) {
85
86 System.out.print("(" + (i + 1) + ") ");
87 n = (int) (Math.random() * 3 + 2);// 2-4个操作数
88 for (int j = 0; j < n; j++) {
89 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值
90 }
91 for (int k = 0; k < n - 1; k++) {
92 no[k] = (int) (Math.random() * useno + 1);// 随机产生操作符
93 }
94 for (int h = 0; h < n; h++) {
95 if (h != n - 1) {
96 System.out.print(num1[h]);
97 System.out.print(op[no[h]]);
98 } else {
99 System.out.print(num1[h] + "=");
100 }
101 }
102 System.out.println();
103
104 // 计算答案
105 left = 0;
106 right = num1[0];
107 sign = 1;
108
109 for (int g = 0; g < n - 1; g++) {
110 switch (op[no[g]]) {
111 case '+':
112 left = left + sign * right;
113 sign = 1;
114 right = num1[g + 1];
115 break;
116 case '-':
117 left = left + sign * right;
118 sign = -1;
119 right = num1[g + 1];
120 break;
121 case '*':
122 right = right * num1[g + 1];
123 break;
124 case '/':
125 right = right / num1[g + 1];
126 break;
127 }
128 }
129 staticanser[i] = String
130 .valueOf(decimal.format(left + sign * right));
131 // System.out.println((i+1)+":"+staticanser[i]);
132 }
133
134 // 学生答题模块
135 System.out.println("==================答题分割线=========================");
136 for (int i = 0; i < max; i++) {
137 System.out.print((i + 1) + ":");
138 useranser[i] = in.next();
139 if (Float.parseFloat(useranser[i]) == Float
140 .parseFloat(staticanser[i])) {
141 count++;
142 }
143 }
144 System.out.println("标准答案为:");
145 for (int i = 0; i < max; i++) {
146 System.out.println((i + 1) + ":" + staticanser[i]);
147 }
148 System.out.println("答题正确率为:" + ((float) count / (float) max) * 100
149 + "%");
150 System.out.println("==================答题分割线=========================");
151
152 if (real == 'Y' || real == 'y') {
153 System.out.println("二、请计算下列真分数算式。");
154 System.out.println();
155 for (int i = 0; i < max; i++) {
156 System.out.print("(" + (i + 1) + ") ");
157 for (int j = 0; j < 2; j++)// (第一个真分数)
158 {
159 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值
160 if (j == 1 && num1[1] != 0) {
161 while (num1[j - 1] > num1[j]) {
162 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值
163 }
164 }
165 }
166 for (int j = 2; j < 4; j++)// (第二个真分数)
167 {
168 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值
169 if (j == 3 && num1[3] != 0) {
170 while (num1[j - 1] > num1[j]) {
171 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值
172 }
173 }
174 }
175
176 for (int k = 0; k < 1; k++) {// 符号个数
177 no[k] = (int) (Math.random() * useno + 1);// 随机产生操作符
178 }
179 for (int h = 0; h < 4; h++) {// 2个真分数
180 if (h % 2 == 0)
181 System.out.print(("(" + num1[h] + "/"));
182 else if (h % 2 == 1) {
183 System.out.print(num1[h] + ")");
184 if (f < 1) {// 控制只输出一个操作符
185 System.out.print(op[no[f]]);
186 f++;
187 } else
188 System.out.println("=");
189
190 }
191 }
192
193 f = 0;
194 // 计算标准答案
195
196 for (int g = 0; g < 1; g++) {
197 s1 = num1[0] * num1[3];
198 ss1 = num1[1] * num1[2];
199 s2 = num1[1] * num1[3];
200 switch (op[no[g]]) {
201 case '+':
202 result = s1 + ss1;
203 gys = calcMaxSubmultiple(result, s2);// 除以公约数得到最简分数
204 // System.out.println(gys);
205 zjfz = String.valueOf(result / gys);
206 zjfm = String.valueOf(s2 / gys);
207 break;
208 case '-':
209 result = s1 - ss1;
210 gys = calcMaxSubmultiple(result, s2);
211 //System.out.println(gys);
212 zjfz = String.valueOf(result / gys);
213 zjfm = String.valueOf(s2 / gys);
214 break;
215 case '*':
216 result = num1[0] * num1[2];
217 gys = calcMaxSubmultiple(result, s2);
218 // System.out.println(gys);
219 zjfz = String.valueOf(result / gys);
220 zjfm = String.valueOf(s2 / gys);
221 break;
222 case '/':// 乘以倒数
223 result = num1[0] * num1[3];
224 s2 = num1[1] * num1[2];
225 gys = calcMaxSubmultiple(result, s2);
226 if (num1[0] == 0 || num1[2] == 0) {
227 ff = 1;
228 }
229 zjfz = String.valueOf(result / gys);
230 zjfm = String.valueOf(s2 / gys);
231 break;
232 }
233 }
234 if (ff == 1) {
235 staticanser[i] = "0";
236 } else {
237 staticanser[i] = zjfz + "/" + zjfm;
238 }
239
240 // System.out.println((i+1)+":"+staticanser[i]);
241 }
242
243 // 答题模板
244 System.out
245 .println("==================答题分割线=========================");
246 for (int i = 0; i < max; i++) {
247 System.out.print((i + 1) + ":");
248 useranser[i] = in.next();
249 if (useranser[i].equals(staticanser[i])) {
250 count++;
251 }
252 }
253 System.out.println("标准答案为:");
254 for (int i = 0; i < max; i++) {
255 System.out.println((i + 1) + ":" + staticanser[i]);
256 }
257 System.out.println("答题正确率为:" + ((float) count / (float) max) * 100
258 + "%");
259 System.out
260 .println("==================答题分割线=========================");
261 }
262
263 }
264 }
遇到的问题:算最简分数时,有些分数时最简的,有些却不是。有些小数会自动四舍五入,有些却不能。太奇怪了!!真是太奇怪了!!!
最大的收获是学会了思考,学会了和朋友讨论,学会更好地运用网络资源解决问题。
在这次实验中,我觉得每天真是超级充实啊。每天都在出错和改代码之间徘徊。有时候也会想,就这么小一个程序就这么犯愁,要是遇到困难就觉得烦,觉得不想做,那么以后还能做什么。想当一个程序员就一定要经历“傻逼-苦逼-牛逼“这个过程。