小学生口算题卡程序——计应193第五组尚凯旋
一、 计划
1、使用随机函数产生一组(数量可自选)算数题
2、显示算题界面,开始、完成界面,
3、能根据学生的年级出不同的题目。
4、可以进行单项训练,和混合训练,并可以进行打分。
二、开发
1、需求分析:
作为一个一年级小学生的家长,我希望制作一个出题软件,完成100以内的正整数的加减法题随机产生,以便减轻我的家庭负担。
2、生成设计文档:
①创建一个计算生成器类
②分别写menu1和menu2方法,输出选项信息。提高程序对用户的友好性。
③分别写出加减乘除的实现方法。另外还有混合选项,用来更系统的训练小朋友,让小朋友能更灵活的使用加减乘除
④最后写ONE和TWO方法,用来区分一二年级的习题。
代码执行顺序是:main--->ONE 或 TWO ----> 调用menu方法 --->通过选择语句判断进行哪项训练
3、代码规范
要注意命名规则,不要随意命名,大括号的对应。
4、具体设计
编写一个名为For的测试类,然后在main方法里面写关于这个项目的所有方法,运行测试类查看结果。
3、具体编码
1 import java.util.Scanner; 2 import java.lang.String; 3 import java.lang.Math; 4 5 public class CalculateGenerator2 { 6 public static final int one = 1; 7 public static final int two = 2; 8 9 10 public static int score = 0; 11 12 // public static final int score = 0; 13 public static void main(String args[]) { 14 int choose,clas,n,result, x, y,score=0; 15 16 Scanner sr=new Scanner(System.in); 17 System.out.println("欢迎使用计算机"); 18 System.out.println("请你输入您家孩子的年级:"); 19 clas=sr.nextInt(); 20 21 while(true) { 22 if (clas==1) 23 { 24 ONE(); 25 break; 26 } 27 28 if (clas==2) 29 { 30 TWO(); 31 break; 32 } 33 34 } 35 } 36 public static void menu1() { 37 System.out.println("1---------加法"); 38 System.out.println("2---------减法"); 39 System.out.println("3---------退出"); 40 System.out.println("请输入你的选项:"); 41 } 42 public static void menu2() { 43 System.out.println("1---------加法"); 44 System.out.println("2---------减法"); 45 System.out.println("3---------乘法"); 46 System.out.println("4---------除法"); 47 System.out.println("5---------混合运算"); 48 System.out.println("6---------退出"); 49 System.out.println("请输入你的选项:"); 50 } 51 public static int ADD() { 52 int choose,clas,n,result, x, y,score=0; 53 54 Scanner sr=new Scanner(System.in); 55 56 System.out.println("这里是加法运算,请你输入要做几题"); 57 n=sr.nextInt(); 58 while(n>0) { 59 x=(int)(Math.random()*10); 60 y=(int)(Math.random()*10); 61 System.out.print(x+"+"+y+"="); 62 result=sr.nextInt(); 63 if(result==(x+y)) { 64 System.out.println("答案正确,你真棒!"); 65 score++; 66 }else { 67 System.out.println("答案错误,正确答案为"+(x+y)); 68 } 69 n--; 70 } 71 return score; 72 } 73 74 public static int SUB() { 75 int choose,clas,n,result, x, y,score=0; 76 77 Scanner sr=new Scanner(System.in); 78 79 System.out.println("这里是减法运算,请你输入要做几题"); 80 n=sr.nextInt(); 81 while(n>0) { 82 x=(int)(Math.random()*100); 83 y=(int)(Math.random()*100); 84 if(x<y) { 85 System.out.print(y+"-"+x+"="); 86 result=sr.nextInt(); 87 if(result==(y-x)) { 88 System.out.println("答案正确,你真棒!"); 89 score++; 90 }else { 91 System.out.println("答案错误,正确答案为"+(y-x)); 92 } 93 }else { 94 System.out.print(x+"-"+y+"="); 95 result=sr.nextInt(); 96 if(result==(x-y)) { 97 System.out.println("答案正确,你真棒!"); 98 }else { 99 System.out.println("答案错误,正确答案为"+(x-y)); 100 } 101 } 102 n--; 103 } 104 return score; 105 } 106 107 public static int MULTI() { 108 int choose,clas,n,result, x, y,score=0; 109 110 Scanner sr=new Scanner(System.in); 111 112 System.out.println("这里是乘法运算,请你输入要做几题"); 113 n=sr.nextInt(); 114 while(n>0) { 115 x=(int)(Math.random()*10); 116 y=(int)(Math.random()*10); 117 System.out.print(x+"*"+y+"="); 118 result=sr.nextInt(); 119 if(result==(x*y)) { 120 System.out.println("答案正确,你真棒!"); 121 score++; 122 }else { 123 System.out.println("答案错误,正确答案为"+(x*y)); 124 } 125 n--; 126 } 127 return score; 128 } 129 130 public static int DIVI() { 131 int choose,clas,n,result, x, y,score=0; 132 133 Scanner sr=new Scanner(System.in); 134 135 int all, max; 136 System.out.println("这里是除法运算,请你输入要做几题"); 137 n=sr.nextInt(); 138 while(n>0) { 139 x=(int)(Math.random()*10); 140 y=(int)(Math.random()*10); 141 all=x*y; 142 143 if(y!=0) { 144 System.out.print(all+"/"+y+"="); 145 result=sr.nextInt(); 146 if(result==(all/y)) { 147 System.out.println("答案正确,你真棒!"); 148 score++; 149 }else { 150 System.out.println("答案错误,正确答案为"+(all/y)); 151 } 152 } 153 n--; 154 } 155 return score; 156 } 157 158 public static int MIX() { 159 int choose,clas,n,result, x, y,score = 0; 160 161 Scanner sr=new Scanner(System.in); 162 163 System.out.println("这里是混合运算,请你输入要做几题"); 164 n=sr.nextInt(); 165 while(n>0) { 166 int sum=0; 167 x=(int)(Math.random()*10); 168 y=(int)(Math.random()*10); 169 choose=(int)(Math.random()*3); 170 switch(choose) { 171 case 0: 172 System.out.print(x+"+"+y+"="); 173 sum=x+y; 174 break; 175 176 case 1: 177 if(x<y) { 178 System.out.print(y+"-"+x+"="); 179 sum=y-x; 180 }else { 181 System.out.print(x+"-"+y+"="); 182 sum=x-y; 183 } 184 break; 185 186 case 2: 187 System.out.print(x+"*"+y+"="); 188 sum=x*y; 189 break; 190 191 case 3: 192 int all1; 193 all1=x*y; 194 if(y!=0) { 195 System.out.print(all1+"/"+y+"="); 196 sum=all1/y; 197 } 198 break; 199 } 200 201 System.out.print("请输入答案:"); 202 result=sr.nextInt(); 203 if(result==sum) { 204 System.out.println("答案正确,你真棒!"); 205 score++; 206 }else { 207 System.out.println("答案错误,正确答案为"+(sum)); 208 } 209 n--; 210 } 211 return score; 212 } 213 214 public static void ONE() { 215 int choose,clas,n,result, x, y; 216 Scanner sr=new Scanner(System.in); 217 218 while(true) { 219 menu1(); 220 choose=sr.nextInt(); 221 222 switch(choose) { 223 case 1: 224 System.out.println("本次加法运算得分是"+ADD()); 225 break; 226 227 case 2: 228 System.out.println("本次减法运算得分是"+SUB()); 229 break; 230 231 case 3: 232 System.exit(0); 233 } 234 } 235 } 236 237 public static void TWO() { 238 int choose,clas,n,result, x, y; 239 Scanner sr=new Scanner(System.in); 240 241 while(true) { 242 menu2(); 243 choose=sr.nextInt(); 244 245 switch(choose) { 246 case 1: 247 System.out.println("本次加法运算得分是"+ADD()); 248 break; 249 250 case 2: 251 System.out.println("本次减法运算得分是"+SUB()); 252 break; 253 254 case 3: 255 System.out.println("本次乘法运算得分是"+MULTI()); 256 break; 257 258 case 4: 259 System.out.println("本次除法运算得分是"+DIVI()); 260 break; 261 262 case 5: 263 System.out.println("本次运算混合得分是"+MIX()); 264 break; 265 266 case 6: 267 System.exit(0); 268 } 269 } 270 271 } 272 }
4.代码复审
程序逻辑通顺,无语法错误。个别地方功能有些局限性,需要逐步改进。
测试:


总结:程序已可以完成简单的四则运算。但不能同时处理加减乘除的运算,只能把他们分开来分别执行加、减、乘、除的运算。并且不能进行小数的运算。测试时需要手动进行测试。
|
PSP阶段 |
自己所花时间百分比 |
工程师所花时间百分比 |
|
计划 |
10 |
6 |
|
10 |
6 |
|
开发 |
84 |
88 |
|
7 |
10 |
|
5 |
6 |
|
4 |
6 |
|
4 |
3 |
|
10 |
12 |
|
36 |
21 |
|
6 |
9 |
|
12 |
21 |
|
报告 |
6 |
6 |
|
2 |
2 |
|
2 |
1 |
|
2 |
3 |
package KouSuan;
import java.util.Scanner;import java.lang.String;import java.lang.Math;
public class CalculateGenerator2 { public static final int one = 1; public static final int two = 2;
public static int score = 0; // public static final int score = 0; public static void main(String args[]) {int choose,clas,n,result, x, y,score=0;Scanner sr=new Scanner(System.in);System.out.println("欢迎使用计算机");System.out.println("请你输入您家孩子的年级:");clas=sr.nextInt();while(true) {if (clas==1){ONE();break;}if (clas==2){TWO();break;}}} public static void menu1() { System.out.println("1---------加法");System.out.println("2---------减法");System.out.println("3---------退出");System.out.println("请输入你的选项:"); } public static void menu2() { System.out.println("1---------加法");System.out.println("2---------减法");System.out.println("3---------乘法");System.out.println("4---------除法");System.out.println("5---------混合运算");System.out.println("6---------退出");System.out.println("请输入你的选项:"); } public static int ADD() { int choose,clas,n,result, x, y,score=0; Scanner sr=new Scanner(System.in); System.out.println("这里是加法运算,请你输入要做几题");n=sr.nextInt();while(n>0) {x=(int)(Math.random()*10);y=(int)(Math.random()*10);System.out.print(x+"+"+y+"=");result=sr.nextInt();if(result==(x+y)) {System.out.println("答案正确,你真棒!");score++;}else {System.out.println("答案错误,正确答案为"+(x+y));}n--;}return score; } public static void SUB() {int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); System.out.println("这里是减法运算,请你输入要做几题");n=sr.nextInt();while(n>0) {x=(int)(Math.random()*100);y=(int)(Math.random()*100);if(x<y) {System.out.print(x+"-"+y+"=");result=sr.nextInt();if(result==(y-x)) {System.out.println("答案正确,你真棒!");}else {System.out.println("答案错误,正确答案为"+(y-x));}}else {System.out.print(x+"-"+y+"=");result=sr.nextInt();if(result==(x-y)) {System.out.println("答案正确,你真棒!");}else {System.out.println("答案错误,正确答案为"+(x-y));}}n--;} }
public static void MULTI() { int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); System.out.println("这里是乘法运算,请你输入要做几题");n=sr.nextInt();while(n>0) {x=(int)(Math.random()*10);y=(int)(Math.random()*10);System.out.print(x+"*"+y+"=");result=sr.nextInt();if(result==(x*y)) {System.out.println("答案正确,你真棒!");}else {System.out.println("答案错误,正确答案为"+(x*y));}n--;} } public static void DIVI() { int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); int all, max;System.out.println("这里是除法运算,请你输入要做几题");n=sr.nextInt();while(n>0) {x=(int)(Math.random()*10);y=(int)(Math.random()*10);all=x*y;if(y!=0) {System.out.print(all+"/"+y+"=");result=sr.nextInt();if(result==(all/y)) {System.out.println("答案正确,你真棒!");}else {System.out.println("答案错误,正确答案为"+(all/y));}}n--;} } public static void MIX() { int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); System.out.println("这里是混合运算,请你输入要做几题");n=sr.nextInt();while(n>0) {int sum=0;x=(int)(Math.random()*10);y=(int)(Math.random()*10);choose=(int)(Math.random()*3);switch(choose) {case 0:System.out.print(x+"+"+y+"=");sum=x+y; break; case 1:if(x<y) {System.out.print(y+"-"+x+"=");sum=y-x;}else {System.out.print(x+"-"+y+"=");sum=x-y;}break;case 2:System.out.print(x+"*"+y+"=");sum=x*y;break;case 3:int all1;all1=x*y;if(y!=0) {System.out.print(all1+"/"+y+"=");sum=all1/y;}break;}System.out.print("请输入答案:");result=sr.nextInt();if(result==sum) {System.out.println("答案正确,你真棒!");}else {System.out.println("答案错误,正确答案为"+(sum));}n--;} } public static void ONE() { int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); while(true) { menu1(); choose=sr.nextInt(); switch(choose) { case 1: ADD(); System.out.println("本次加法运算得分是"+score); break; case 2: SUB(); break; case 3: System.exit(0); } } } public static void TWO() { int choose,clas,n,result, x, y; Scanner sr=new Scanner(System.in); while(true) { menu2(); choose=sr.nextInt(); switch(choose) { case 1: ADD(); break; case 2: SUB(); break; case 3: MULTI(); break; case 4: DIVI(); break; case 5: MIX(); break; case 6: System.exit(0); } } }}

浙公网安备 33010602011771号