小学生口算题卡程序——计应193第五组何家俊
计划:
能够用java语言做出实现加减乘除的四则运算。
需求分析:
(1) 程序正常工作,能自定义产生题目供学生进行运算。
(2) 每次做完一种类型的题目之后都会得到相应的分数。
1 package RunJianKaiFa; 2 import java.util.Scanner; 3 import java.lang.String; 4 import java.lang.Math; 5 6 public class CalculateGenerator { 7 public static final int one = 1; 8 public static final int two = 2; 9 10 11 // public static int score = 0; 12 13 // public static final int score = 0; 14 public static void main(String args[]) { 15 int choose,clas,n,result, x, y,score=0; 16 17 Scanner sr=new Scanner(System.in); 18 System.out.println("欢迎使用计算机"); 19 System.out.println("请你输入您家孩子的年级:"); 20 clas=sr.nextInt(); 21 22 while(true) { 23 if (clas==1) 24 { 25 ONE(); 26 break; 27 } 28 29 if (clas==2) 30 { 31 TWO(); 32 break; 33 } 34 35 } 36 } 37 public static void menu1() { 38 System.out.println("1---------加法"); 39 System.out.println("2---------减法"); 40 System.out.println("3---------退出"); 41 System.out.println("请输入你的选项:"); 42 } 43 public static void menu2() { 44 System.out.println("1---------加法"); 45 System.out.println("2---------减法"); 46 System.out.println("3---------乘法"); 47 System.out.println("4---------除法"); 48 System.out.println("5---------混合运算"); 49 System.out.println("6---------退出"); 50 System.out.println("请输入你的选项:"); 51 } 52 public static int ADD() { 53 int choose,clas,n,result, x, y,score=0; 54 55 Scanner sr=new Scanner(System.in); 56 57 System.out.println("这里是加法运算,请你输入要做几题"); 58 n=sr.nextInt(); 59 while(n>0) { 60 x=(int)(Math.random()*10); 61 y=(int)(Math.random()*10); 62 System.out.print(x+"+"+y+"="); 63 result=sr.nextInt(); 64 if(result==(x+y)) { 65 System.out.println("答案正确,你真棒!"); 66 score++; 67 }else { 68 System.out.println("答案错误,正确答案为"+(x+y)); 69 } 70 n--; 71 } 72 return score; 73 } 74 75 public static int SUB() { 76 int choose,clas,n,result, x, y,score=0; 77 78 Scanner sr=new Scanner(System.in); 79 80 System.out.println("这里是减法运算,请你输入要做几题"); 81 n=sr.nextInt(); 82 while(n>0) { 83 x=(int)(Math.random()*100); 84 y=(int)(Math.random()*100); 85 if(x<y) { 86 System.out.print(y+"-"+x+"="); 87 result=sr.nextInt(); 88 if(result==(y-x)) { 89 System.out.println("答案正确,你真棒!"); 90 score++; 91 }else { 92 System.out.println("答案错误,正确答案为"+(y-x)); 93 } 94 }else { 95 System.out.print(x+"-"+y+"="); 96 result=sr.nextInt(); 97 if(result==(x-y)) { 98 System.out.println("答案正确,你真棒!"); 99 score++; 100 }else { 101 System.out.println("答案错误,正确答案为"+(x-y)); 102 } 103 } 104 n--; 105 } 106 return score; 107 } 108 109 public static int MULTI() { 110 int choose,clas,n,result, x, y,score=0; 111 112 Scanner sr=new Scanner(System.in); 113 114 System.out.println("这里是乘法运算,请你输入要做几题"); 115 n=sr.nextInt(); 116 while(n>0) { 117 x=(int)(Math.random()*10); 118 y=(int)(Math.random()*10); 119 System.out.print(x+"*"+y+"="); 120 result=sr.nextInt(); 121 if(result==(x*y)) { 122 System.out.println("答案正确,你真棒!"); 123 score++; 124 }else { 125 System.out.println("答案错误,正确答案为"+(x*y)); 126 } 127 n--; 128 } 129 return score; 130 } 131 132 public static int DIVI() { 133 int choose,clas,n,result, x, y,score=0; 134 135 Scanner sr=new Scanner(System.in); 136 137 int all, max; 138 System.out.println("这里是除法运算,请你输入要做几题"); 139 n=sr.nextInt(); 140 while(n>0) { 141 x=(int)(Math.random()*10); 142 y=(int)(Math.random()*10); 143 all=x*y; 144 145 if(y!=0) { 146 System.out.print(all+"/"+y+"="); 147 result=sr.nextInt(); 148 if(result==(all/y)) { 149 System.out.println("答案正确,你真棒!"); 150 score++; 151 }else { 152 System.out.println("答案错误,正确答案为"+(all/y)); 153 } 154 } 155 n--; 156 } 157 return score; 158 } 159 160 public static int MIX() { 161 int choose,clas,n,result, x, y,score=0; 162 163 Scanner sr=new Scanner(System.in); 164 165 System.out.println("这里是混合运算,请你输入要做几题"); 166 n=sr.nextInt(); 167 while(n>0) { 168 int sum=0; 169 x=(int)(Math.random()*10); 170 y=(int)(Math.random()*10); 171 choose=(int)(Math.random()*3); 172 switch(choose) { 173 case 0: 174 System.out.print(x+"+"+y+"="); 175 sum=x+y; 176 break; 177 178 case 1: 179 if(x<y) { 180 System.out.print(y+"-"+x+"="); 181 sum=y-x; 182 }else { 183 System.out.print(x+"-"+y+"="); 184 sum=x-y; 185 } 186 break; 187 188 case 2: 189 System.out.print(x+"*"+y+"="); 190 sum=x*y; 191 break; 192 193 case 3: 194 int all1; 195 all1=x*y; 196 if(y!=0) { 197 System.out.print(all1+"/"+y+"="); 198 sum=all1/y; 199 } 200 break; 201 } 202 203 System.out.print("请输入答案:"); 204 result=sr.nextInt(); 205 if(result==sum) { 206 System.out.println("答案正确,你真棒!"); 207 score++; 208 }else { 209 System.out.println("答案错误,正确答案为"+(sum)); 210 } 211 n--; 212 } 213 return score; 214 } 215 216 public static void ONE() { 217 int choose,clas,n,result, x, y; 218 Scanner sr=new Scanner(System.in); 219 220 while(true) { 221 menu1(); 222 choose=sr.nextInt(); 223 224 switch(choose) { 225 case 1: 226 System.out.println("本次加法运算得分是"+ADD()); 227 break; 228 229 case 2: 230 SUB(); 231 System.out.println("本次减法运算得分是"+SUB()); 232 break; 233 234 case 3: 235 System.exit(0); 236 } 237 } 238 } 239 240 public static void TWO() { 241 int choose,clas,n,result, x, y; 242 Scanner sr=new Scanner(System.in); 243 244 while(true) { 245 menu2(); 246 choose=sr.nextInt(); 247 248 switch(choose) { 249 case 1: 250 System.out.println("本次加法运算得分是"+ADD()); 251 break; 252 253 case 2: 254 System.out.println("本次减法运算得分是"+SUB()); 255 break; 256 257 case 3: 258 System.out.println("本次乘法运算得分是"+MULTI()); 259 break; 260 261 case 4: 262 System.out.println("本次除法运算得分是"+DIVI()); 263 break; 264 265 case 5: 266 System.out.println("本次混合运算得分是"+MIX()); 267 break; 268 269 case 6: 270 System.exit(0); 271 System.out.println("已退出"); 272 } 273 } 274 275 }
代码规范:
定义类时要选取合适的名字,要规范每一行代码,让每个看到这些代码的人都能清楚地了解这些代码的意思,也是为了后期修改代码时更加方便、快捷。
代码复审:
仔细检查代码,看是否存在错误或代码不规范的,如果有要及时改正。
总结:
程序已可以完成简单的四则运算。但是不能进行小数的运算。测试时需要手动进行测试。
PSP阶段 |
自己所花时间百分比 |
工程师所花时间百分比 |
计划 |
11 |
6 |
|
11 |
6 |
开发 |
83 |
88 |
|
6 |
10 |
|
5 |
6 |
|
4 |
6 |
|
5 |
3 |
|
9 |
12 |
|
36 |
21 |
|
6 |
9 |
|
12 |
21 |
报告 |
6 |
6 |
|
2 |
2 |
|
2 |
1 |
|
2 |
3 |