四则运算3
要求:
程序能夠接受用戶輸入答案,并判斷對錯,最后輸出對錯個數(shù)
一、解題思路
1、在輸出算術題的時候加入用戶輸入語句,并用if語句來判斷對錯。
2、新建一個二維數(shù)組b[][],用count1,count2分別對錯題和對題的進行計數(shù),并將錯誤的題放入數(shù)組b[][]中。
3、將數(shù)組b[][]內(nèi)的錯題輸出。
二、程序源碼
1 package szys;
2 import java.util.Scanner;
3 import java.util.Random;
4 public class szys2
5 {
6
7 public static void main(String[] args)
8 {
9 // TODO 自動生成的方法存根
10 System.out.println("********四則運算*********");
11 System.out.println("******1.加減運算*********");
12 System.out.println("******2.加減乘除運算******");
13 System.out.println("**********************");
14 Scanner n=new Scanner(System.in);
15 System.out.print("請輸入運算選項: ");
16 int n1=n.nextInt();
17 switch(n1)
18 {
19 case 1:add();
20 break;
21 case 2:mul();
22 break;
23 }
24 }
25
26 public static void add()
27 { //加減法
28 System.out.println("******加法運算******");
29 int i,count1=0,count2=0;
30 char[] ch={'+','-',};//字符數(shù)組
31 int a[][]=new int[10000][3];
32 int b[][]=new int[10000][4];
33 Random r=new Random();
34 Scanner s=new Scanner(System.in);
35 int n1 = 0;
36 System.out.print("請輸入算術題的個數(shù): ");
37 int m1=s.nextInt();
38 //System.out.print("請輸入每行打印的個數(shù): ");
39 //int m2=s.nextInt();
40 System.out.print("加減是否有負數(shù)(1/0):");
41 int c1=s.nextInt();
42 if(c1==1)
43 {
44 System.out.println("請選擇數(shù)值范圍:");
45 int a1=s.nextInt();
46 int a2=s.nextInt();
47 int count=0;
48 System.out.println("******算數(shù)如下******");
49 while(count<m1)
50 {
51 boolean m=true;
52 int s1=(r.nextInt(a2-a1+1)+a1);
53 int s2=(r.nextInt(a2-a1+1)+a1);
54 int s3=(r.nextInt(ch.length));
55 for(i=0;i<=count;i++)
56 {
57 if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
58 {
59 m=false;
60 }
61 }
62 if(m=true)
63 {
64 a[count][0]=s1;
65 a[count][1]=s2;
66 a[count][2]=s3;
67 }
68 else
69 {
70 count--;
71 }
72 count++;
73 }
74 for(i=0;i<m1;i++)
75 {
76 switch(a[i][2])
77 {
78 case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+" ");
79 break;}
80 case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+" ");
81 break;}
82 }
83 Scanner s1=new Scanner(System.in);
84 n1=s.nextInt();
85 if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
86 {
87 count1++;
88 }
89 else
90 {
91 b[count2][0]=a[i][0];
92 b[count2][1]=a[i][1];
93 b[count2][2]=a[i][2];
94 b[count2][3]=n1;
95 count2++;
96 }
97 }
98 System.out.println("測試結果如下:");
99 System.out.println("共"+m1+"道題,答對"+count1+"道題,答錯"+count2+"道題");
100 System.out.println("錯題如下:");
101 for(i=0;i<count2;i++)
102 {
103 switch(b[i][2])
104 {
105 case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+" ");
106 break;}
107 case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+" ");
108 break;}
109 }
110 }
111 }
112 else
113 {
114 System.out.println("請選測數(shù)值范圍:");
115 int a1=s.nextInt();
116 int a2=s.nextInt();
117 int count=0;
118 while(count<m1)
119 {
120 boolean m=true;
121 int s1=(r.nextInt(a2-a1+1)+a1);
122 int s2=(r.nextInt(a2-a1+1)+a1);
123 int s3=(r.nextInt(ch.length));
124 for(i=0;i<=count;i++)
125 {
126 if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
127 {
128 m=false;
129 }
130 }
131 if(m=true)
132 {
133 a[count][0]=s1;
134 a[count][1]=s2;
135 a[count][2]=s3;
136 }
137 else
138 {
139 count--;
140 }
141 count++;
142 }
143 for(i=0;i<m1;i++)
144 {
145 switch(a[i][2])
146 {
147 case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+" ");
148 break;}
149 case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+" ");
150 break;}
151 }
152 n1=s.nextInt();
153 if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
154 {
155 count1++;
156 }
157 else
158 {
159 b[count2][0]=a[i][0];
160 b[count2][1]=a[i][1];
161 b[count2][2]=a[i][2];
162 b[count2][3]=n1;
163 count2++;
164 }
165 }
166 System.out.println("測試結果如下:");
167 System.out.println("共"+m1+"道題,答對"+count1+"道題,答錯"+count2+"道題");
168 System.out.println("錯題如下:");
169 for(i=0;i<count2;i++)
170 {
171 switch(b[i][2])
172 {
173 case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+" ");
174 break;}
175 case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+" ");
176 break;}
177 }
178 }
179 }
180 }
181
182 public static void mul()
183 { //乘除法
184 System.out.println("******乘除法運算******");
185 int i,count1=0,count2=0;
186 Random r=new Random();
187 char[] ch={'+','-','*','/'};//字符數(shù)組
188 int a[][]=new int[10000][3];
189 int b[][]=new int[10000][4];
190 Scanner s=new Scanner(System.in);
191 int n1 = 0;
192 System.out.print("請輸入算術題的個數(shù): ");
193 int m1=s.nextInt();
194 //System.out.print("請輸入每行打印的個數(shù): ");
195 //int m3=s.nextInt();
196 System.out.print("加減是否有負數(shù)(1/0):");
197 int c2=s.nextInt();
198 if(c2==1)
199 {
200 System.out.println("請選測數(shù)值范圍:");
201 int a1=s.nextInt();
202 int a2=s.nextInt();
203 int count=0;
204 while(count<m1)
205 {
206 boolean m=true;
207 int s1=(r.nextInt(a2-a1+1)+a1);
208 int s2=(r.nextInt(a2-a1+1)+a1);
209 int s3=(r.nextInt(ch.length));
210 for(i=0;i<=count;i++)
211 {
212 if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
213 {
214 m=false;
215 }
216 }
217 if(m=true)
218 {
219 a[count][0]=s1;
220 a[count][1]=s2;
221 a[count][2]=s3;
222 }
223 else
224 {
225 count--;
226 }
227 count++;
228 }
229 for(i=0;i<m1;i++)
230 {
231 switch(a[i][2])
232 {
233 case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+" ");
234 break;}
235 case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+" ");
236 break;}
237 case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+" ");
238 break;}
239 case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+" ");
240 break;}
241 }
242 n1=s.nextInt();
243 if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
244 {
245 count1++;
246 }
247 else
248 {
249 b[count2][0]=a[i][0];
250 b[count2][1]=a[i][1];
251 b[count2][2]=a[i][2];
252 b[count2][3]=n1;
253 count2++;
254 }
255 }
256 System.out.println("測試結果如下:");
257 System.out.println("共"+m1+"道題,答對"+count1+"道題,答錯"+count2+"道題");
258 System.out.println("錯題如下:");
259 for(i=0;i<count2;i++)
260 {
261 switch(b[i][2])
262 {
263 case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+" ");
264 break;}
265 case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+" ");
266 break;}
267 case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+" ");
268 break;}
269 case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+" ");
270 break;}
271 }
272 }
273
274 }
275 else
276 {
277 System.out.println("請選測數(shù)值范圍:");
278 int a1=s.nextInt();
279 int a2=s.nextInt();
280 int count=0;
281 while(count<m1)
282 {
283 boolean m=true;
284 int s1=(r.nextInt(a2-a1+1)+a1);
285 int s2=(r.nextInt(a2-a1+1)+a1);
286 int s3=(r.nextInt(ch.length));
287 for(i=0;i<=count;i++)
288 {
289 if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
290 {
291 m=false;
292 }
293 }
294 if(m=true)
295 {
296 a[count][0]=s1;
297 a[count][1]=s2;
298 a[count][2]=s3;
299 }
300 else
301 {
302 count--;
303 }
304 count++;
305 }
306 for(i=0;i<m1;i++)
307 {
308 switch(a[i][2])
309 {
310 case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+" ");
311 break;}
312 case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+" ");
313 break;}
314 case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+" ");
315 break;}
316 case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+" ");
317 break;}
318 }
319 n1=s.nextInt();
320 if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
321 {
322 count1++;
323 }
324 else
325 {
326 b[count2][0]=a[i][0];
327 b[count2][1]=a[i][1];
328 b[count2][2]=a[i][2];
329 b[count2][3]=n1;
330 count2++;
331 }
332 }
333 System.out.println("測試結果如下:");
334 System.out.println("共"+m1+"道題,答對"+count1+"道題,答錯"+count2+"道題");
335 System.out.println("錯題如下:");
336 for(i=0;i<count2;i++)
337 {
338 switch(b[i][2])
339 {
340 case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+" ");
341 break;}
342 case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+" ");
343 break;}
344 case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+" ");
345 break;}
346 case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+" ");
347 break;}
348 }
349 }
350 }
351 }
352 }
三、運行結果
四、總結
通過這次練習將自己程序又進一步的完善,滿足用戶的基本要求,但還有很多地方需要在日后的學習中去完善;
比如:除法運算可以精確到小數(shù)的位數(shù)等等。在今后的編寫程序的時候會考慮好問題的每一個細節(jié)。
五、項目計劃表
|
項目計劃總結 周活動總結表 姓名:趙玉璇 日期:2015/3/22 |
|||||||
|
任務 日期 |
聽課 |
編程 |
閱讀課外數(shù)目 |
鍛煉身體 |
寫老師留得作業(yè) |
業(yè)余活動 |
日總計 (分鐘) |
|
周日 |
180 |
120 |
60 |
360 |
|||
|
周一 |
60 |
60 |
30 |
60 |
120 |
330 |
|
|
周二 |
100 |
120 |
30 |
120 |
120 |
580 |
|
|
周三 |
120 |
60 |
180 |
360 |
|||
|
周四 |
90 |
90 |
30 |
60 |
120 |
390 |
|
|
周五 |
100 |
100 |
30 |
120 |
350 |
||
|
周六 |
240 |
120 |
180 |
540 |
|||
|
階段時間和效率 周數(shù):1 |
|||||||
|
總計 |
200 |
910 |
330 |
180 |
600 |
600 |
2940 |
|
平均 |
30 |
120 |
50 |
30 |
90 |
90 |
400 |
|
最大 |
100 |
240 |
120 |
120 |
120 |
180 |
540 |
|
最小 |
100 |
60 |
30 |
30 |
60 |
120 |
330 |
2,時間記錄日志:
學生:趙玉璇 日期:2015/3/22
老師:王建民 課程:PSP
|
日期 |
開始時間 |
結束時間 |
中斷時間 |
凈時間 |
活動 |
備注 |
|
3/17 |
08:00 |
12:00 |
40 |
200 |
上課 |
|
|
14:30 |
18:30 |
40 |
200 |
編程 |
||
|
19:00 |
20:00 |
10 |
120 |
寫作業(yè) |
||
|
22:00 |
22:30 |
30 |
讀大道至簡 |
|||
|
3/18 |
08:00 |
10:00 |
20 |
100 |
聽課 |
|
|
11:00 |
12:00 |
60 |
編程 |
|||
|
14:00 |
15:00 |
60 |
寫作業(yè) |
|||
|
21:00 |
21:30 |
30 |
讀夢斷代碼 |
|||
|
3/19 |
10:00 |
12:00 |
20 |
100 |
聽課 |
|
|
14:00 |
14:30 |
30 |
編程 |
|||
|
15:00 |
16:00 |
60 |
寫作業(yè) |
|||
|
21:00 |
21:30 |
30 |
讀夢斷代碼 |
|||
|
3/20 |
08:00 |
12:00 |
40 |
200 |
聽課 |
|
|
14:00 |
15:40 |
100 |
編程 |
|||
|
21:00 |
21:30 |
30 |
讀夢斷代碼 |
|||
|
3/21 |
08:00 14:30 18:00 |
11:00 17:00 20:30 |
180 150 150 |
休息 學習 編程 |
3,缺陷記錄日志:
學生:趙玉璇 日期:2015/3/22
老師:王建民
|
日期 |
編號 |
類型 |
引入階段 |
排除階段 |
修復階段 |
修復缺陷 |
|
3/19 |
1 |
01 |
編寫 |
編譯 |
2min |
|
|
括號丟失 |
||||||
|
2 |
||||||
|
3/21 |
3 |
02 |
編寫 |
編譯 |
20min |
|
|
循環(huán)錯誤,計數(shù)有誤 |
||||||
|
4 |
02 |
編寫 |
編譯 |
10min |
||
|
輸出有誤,語法錯誤 |
||||||
總結
- 上一篇: SEO内容为王之如何创造伪原创
- 下一篇: 斜率优化(转载)