设计思想:用随机数来代表运算符号和分数的运算,在分数运算中借用最小公倍数和最大公约数来进行同分从而使得结果是最简的。
程序源代码:
package 四则运算;
2 import java.util.*;
3 public class suanshi {
4 public static void main(String args[]){
5 System.out.println("请输入要生成的数学题的数目:" );
6
7 Scanner scanner = new Scanner(System.in);
8 int n = scanner.nextInt();
9
10 //随机生成一个代表分数运算还是简单运算的数字
11 int temp = (int)(Math.random()*2)+1;
12 //生成随机数
13 int firstNum = (int)(Math.random()*10);
14 int secondNum = (int)(Math.random()*10);
15 int thirdNum = (int)(Math.random()*10);
16 int fourthNum = (int)(Math.random()*10);
17 //生成随机的1-4来代表加减乘除
18 int index = 1+(int)(Math.random()*4);
19
20 System.out.println("式子中是否含有乘除法:1.是 2.否");
21 Scanner scanner1 = new Scanner(System.in);
22 int cc = scanner.nextInt();
23 if(cc==1)
24 for(int i=0;i<n;i++)
25 {
26
27 if(temp == 1)
28 {
29 //编辑
30 if(index == 1)
31 {
32 System.out.println((i+1)+"."+" "+firstNum+ "+" +secondNum+ "=");
33 Scanner in1 = new Scanner(System.in);
34 int a = in1.nextInt();
35 if(a==firstNum+secondNum)
36 System.out.println("回答正确");
37 else
38 System.out.println("回答错误,正确答案为:"+(firstNum+secondNum));
39 }
40 if(index == 2)
41 {
42 System.out.println((i+1)+"."+" "+firstNum+ "-" +secondNum+ "=");
43 Scanner in2 = new Scanner(System.in);
44 int b = in2.nextInt();
45 if(b==firstNum-secondNum)
46 System.out.println("回答正确");
47 else
48 System.out.println("回答错误,正确答案为:"+(firstNum-secondNum));
49 }
50 if(index == 3)
51 {
52 System.out.println((i+1)+"."+" "+firstNum+ "*" +secondNum+ "=");
53 Scanner in3 = new Scanner(System.in);
54 int a = in3.nextInt();
55 if(a==firstNum*secondNum)
56 System.out.println("回答正确");
57 else
58 System.out.println("回答错误,正确答案为:"+(firstNum*secondNum));
59 }
60 if(index == 4)
61 {
62 if(firstNum!=0)
63 System.out.println((i+1)+"."+" "+secondNum+ "/" +firstNum+ "=");
64 else if(secondNum!=0)
65 System.out.println((i+1)+"."+" "+firstNum+ "/" +secondNum+ "=");
66 else
67 {
68 System.out.println(30 + "/" + 15 + "=?");
69 }
70 Scanner in4 = new Scanner(System.in);
71 double a = in4.nextDouble();
72 if(a==firstNum/secondNum)
73 System.out.println("回答正确");
74 else
75 System.out.println("回答错误,正确答案为:"+(firstNum/secondNum));
76
77 }
78 }
79 //分数的加减运算
80 if(temp==2)
81 {
82 if(index==1)
83 {
84 System.out.println((i+1)+". "+firstNum+"/"+secondNum+" + "+thirdNum+"/"+fourthNum);
85 int a = min(secondNum,fourthNum);//分母的最小公倍数
86 int b = a/secondNum;//通分后第一个式子要乘的数
87 int c = a/fourthNum;//通分后第二个式子的要乘的数
88 int m = firstNum*b;//通分后第一个式子的分子
89 int d = thirdNum*c;//同分后第二个式子的分子
90 int w = m+d;//相加后的分子;
91 int f = max(a,w);
92 double g = (w/f)/(a/f);
93 Scanner in5 = new Scanner(System.in);
94 double h = in5.nextDouble();
95 if(h==g)
96 System.out.println("回答完全正确");
97 else
98 System.out.println("回答错误,正确答案为:"+g);
99 }
100 if(index == 2)
101 {
102 System.out.println((i+1)+". "+firstNum+"/"+secondNum+" - "+thirdNum+"/"+fourthNum);
103 int a = min(secondNum,fourthNum);
104 int b = a/secondNum;//通分后第一个式子要乘的数
105 int c = a/fourthNum;//通分后第二个式子的要乘的数
106 int m = firstNum*b;//通分后第一个式子的分子
107 int d = thirdNum*c;//同分后第二个式子的分子
108 int fenJian = m-d;//相减后的分子
109 int f = max(a,fenJian);
110 double g = (fenJian/f)/(a/f);
111 Scanner in6 = new Scanner(System.in);
112 double h = in6.nextDouble();
113 if(h==g)
114 System.out.println("回答完全正确");
115 else
116 System.out.println("回答错误,正确答案为:"+g);
117 }
118 if(index == 3)
119 {
120 System.out.println((i+1)+". "+firstNum+"/"+secondNum+" * "+thirdNum+"/"+fourthNum);
121 int Sfenzi = firstNum*thirdNum;//相乘后的分子
122 int Sfenmu = secondNum*fourthNum;//相乘后的分子
123 int c = max(Sfenzi,Sfenmu);//分子和分母的最大公约数
124 int Efenzi = Sfenzi/c;
125 int Efenmu = Sfenmu/c;
126 double de = Efenzi/Efenmu;//约分后的得数
127 Scanner in7 = new Scanner(System.in);
128 double h = in7.nextDouble();
129 if(h==de)
130 System.out.println("回答正确");
131 else
132 System.out.println("回答错误,正确答案为:"+de);
133 }
134 if(index == 4)
135 {
136 //分数的除法就是乘以第二个数的倒数
137 if(secondNum!=0&&fourthNum!=0&&thirdNum/fourthNum!=0)
138 System.out.println((i+1)+"."+" "+firstNum+ "/" +secondNum+ " / "+thirdNum+"/"+fourthNum);
139
140 else
141 {
142 System.out.println(30 + "/" + 15 + "=?");
143 }
144 int Sfenzi = firstNum*fourthNum;//分子
145 int Sfenmu = secondNum*thirdNum;//分母
146 int c = max(Sfenzi,Sfenmu);
147 int Efenzi = Sfenzi/c;
148 int Efenmu = Sfenmu/c;
149 double de = Efenzi/Efenmu;
150 Scanner in8 = new Scanner(System.in);
151 double h = in8.nextDouble();
152 if(h==de)
153 System.out.println("回答正确");
154 else
155 System.out.println("回答错误,正确答案为:"+de);
156 }
157
158 }
159 }
160
161
162 }
163
164
165
166
167
168
169 //求最大公约数与最小公倍数
170
171
172 public static int max(int a ,int b)
173 {
174 //循环法求两个数的最大公约数
175
176 //保证第一个参数大于第二个参数
177 if(a<b)
178 {
179 int temp;
180 temp=a;
181 a=b;
182 b=temp;
183 }
184 while(a%b!=0) //在余数不为零时循环
185 {
186 int temp=a%b;
187 a=b;
188 b=temp;
189 }
190 return b; //返回最大公约数
191 }
192
193 public static int min(int a,int b)
194 {
195 //求最小公倍数
196 return a*b/max(a,b);
197 }
198
199 }
程序截图:
上课未及时完后的原因:上课的时候还是对分块化的编程不熟悉,导致对程序不知道如何去写,似乎是无从下手,下课后及时思考了这些让程序得以运行,还有没有想到运算符号如何替代,下课与同学的交流中得到感觉。
反思:要继续多写程序多认真的思考一些,对于模块化编程要继续熟悉。