软件工程概论03
設計思路
1、輸入:輸入選擇;
2、選擇內容:選擇出2位數的題還是多位數的題還是退出程序
? ? ? ? ? ?選擇1是否有乘除法(運算符0-1或0-3);
???????????? 選擇5加減有無負數;
???????????? 選擇6除法有無余數;
???????????? 選擇7是選擇數量或打印方式。
? ? ? ? ? ? ?3是數值范圍的前域
? ? ? ? ? ? ?4是數值范圍的后域
3、加工:2位數的題設置循環,令其長度為一個足夠大的數,滿足程序一次運行可多次使用
????? 是否有乘除法:在前面做出選擇后,在下面只需設置運算符隨機出數的范圍在0-1之間還是0-3之間
????? 數值范圍:即四則運算隨機出數的范圍在前域~后域
???? 加減有無負數:對隨機生成的數字進行運算,如果進行加/減運算之后,有負數,則根據選擇進行保留或舍棄
???? 控制題目不能重復:將之前的題目存放在數組中,然后依次進行比較
? ? ? 打印方式:根據用戶輸入要求一行輸出幾列后,利用取余的方法判斷是否要換行輸出
??????? 計算則按照順序計算即可
? ? 多位數的題:
設置循環,令其長度為一個足夠大的數,滿足程序一次運行可多次使用
????? 是否有乘除法:在前面做出選擇后,在下面只需設置運算符隨機出數的范圍在0-1之間還是0-3之間
????? 數值范圍:即四則運算隨機出數的范圍在前域~后域
???? 加減有無負數:對隨機生成的數字進行運算,如果進行加/減運算之后,有負數,則根據選擇進行保留或舍棄
? ? 有無括號:用隨機數來進行選擇在原來式子之前還是之后進行添加
???? 控制題目不能重復:將之前的題目存放在數組中,然后依次進行比較
? ? 打印方式:根據用戶輸入要求一行輸出幾列后,利用取余的方法判斷是否要換行輸出
??? 帶括號的計算是直接從最里層括號開始往外依次計算
???? 不帶括號的計算則分為沒有乘除法和有乘除法
????? 沒有乘除法的是將運算式存為二維數組,將數值和運算符依次分別存放于兩個數組中,然后按從左到右的順序進行計算
??? 有乘除法的則需進一步統計,在哪有除法或者乘法,然后先進行這兩個運算。
4、輸出:2位數運算,顯示運算式以及用戶輸入的結果,還有系統判斷的結果
??????????? 最后,則是系統統計用戶的做題情況
???????????? 多位數運算則是顯示運算式以及系統產生的結果
?
?
1 import java.util.Random;2 import java.util.Scanner;3 4 import javax.swing.JOptionPane;5 6 //4:007 public class Arithmetic {8 9 public static void main(String[] args) {10 // TODO 自動生成的方法存根11 int c;//選擇12 int c1,c4,c5,c6,c7,c8;13 int a,b,d1;//二位數運算14 int c2,c3;15 int e=0;//運算符16 String s="";17 double d=0.0;//2位數的結果,用戶18 double f=0.0,g=0.0;//2位數結果,系統,多位數結果,系統19 int flag=0;//判斷是否輸出20 int m=0;//題數21 Random rand = new Random();22 for(int i=0;i<100000000;i++)23 {24 System.out.println("請輸入選擇:1、2位數運算 2、多位數運算 3、退出");25 Scanner input=new Scanner(System.in) ;26 c=input.nextInt();27 if(c==1)28 {29 System.out.println("請輸入選擇:1 、有乘除法 2、無乘除法");30 Scanner input1=new Scanner(System.in) ;31 c1=input1.nextInt();32 System.out.println("請輸入數值范圍的前域 ");33 Scanner input2=new Scanner(System.in) ;34 c2=input2.nextInt();35 System.out.println("請輸入數值范圍的后域");36 Scanner input3=new Scanner(System.in) ;37 c3=input3.nextInt();38 System.out.println("請輸入選擇:1、加減有負數 2、加減無負數");39 Scanner input4=new Scanner(System.in) ;40 c4=input4.nextInt();41 System.out.println("請輸入選擇:1、除法有余數 2、除法無余數");42 Scanner input5=new Scanner(System.in) ;43 c5=input5.nextInt();44 System.out.println("請輸入出題數量");45 Scanner input6=new Scanner(System.in) ;46 c6=input6.nextInt();47 System.out.println("請輸入在一行中輸出幾列運算式?");48 Scanner input7=new Scanner(System.in) ;49 c7=input7.nextInt();50 String []Repeat=new String[2*c6];51 for(int w=0;w<c6;w++)52 {53 int w1;54 w1=w;55 //有無乘除法56 if(c1==1)57 {58 e=rand.nextInt(4);59 }60 if(c1==2)61 {62 e=rand.nextInt(2);63 }64 //數值范圍65 a=rand.nextInt(c3)%(c3-c2+1)+c2;66 b=rand.nextInt(c3)%(c3-c2+1)+c2;67 //加減有無負數68 if(c4==1)//有負數69 {70 flag=0;71 }72 if(c4==2)73 { 74 if(e==0)75 {76 if((a+b)>=0)77 {78 flag=0;79 }80 else 81 {82 flag=1;83 }84 }85 if(e==1)86 {87 if((a-b)>=0)88 {89 flag=0;90 }91 else92 {93 flag=1;94 }95 }96 }97 //符號98 if(e==0)99 { 100 s="+"; 101 f=a+b; 102 } 103 if(e==1) 104 { 105 s="-"; 106 f=a-b; 107 } 108 if(e==2) 109 { 110 s="*"; 111 f=a*b; 112 } 113 if(e==3) 114 { 115 if(b!=0) 116 { 117 if(c5==1) 118 { 119 s="/"; 120 f=a/b; 121 } 122 if(c5==2) 123 { 124 if(a%b==0) 125 { 126 s="/"; 127 f=a/b; 128 } 129 if(a%b!=0) 130 { 131 flag=1; 132 } 133 } 134 135 } 136 } 137 //判斷重復 138 if(a<0.0&&b>=0.0) 139 { 140 Repeat[w]="("+a+")"+s+b; 141 } 142 if(a>=0.0&&b<0.0) 143 { 144 Repeat[w]=a+s+"("+b+")"; 145 } 146 if(a<0.0&&b<0.0) 147 { 148 Repeat[w]="("+a+")"+s+"("+b+")"; 149 } 150 if(a>=0.0&&b>=0.0) 151 { 152 Repeat[w]=a+s+b; 153 } 154 for(int w2=0;w2<w1;w2++) 155 { 156 if(Repeat[w].equals(Repeat[w2])) 157 { 158 flag =1; 159 } 160 else 161 {flag =0;} 162 } 163 //打印 164 if(flag==0) 165 { 166 if((w+1)%c7==0) 167 { 168 for(int k=0;k<10000000;k++) 169 { 170 String inputx=JOptionPane.showInputDialog(Repeat[w]+"="+"請輸入計算結果"); 171 if(inputx!=null&&!inputx.equals("")) 172 { 173 d=Double.parseDouble(inputx); 174 break; 175 } 176 } 177 if(d==f) 178 { 179 System.out.println(Repeat[w]+"="+d+"正確"); 180 m++; 181 } 182 if(d!=f) 183 { 184 System.out.println(Repeat[w]+"="+d+"不正確"); 185 } 186 } 187 else 188 { 189 190 for(int k=0;k<10000000;k++) 191 { 192 String inputx=JOptionPane.showInputDialog(Repeat[w]+"="+"請輸入計算結果"); 193 if(inputx!=null&&!inputx.equals("")) 194 { 195 d=Double.parseDouble(inputx); 196 break; 197 } 198 } 199 if(d==f) 200 { 201 System.out.print(Repeat[w]+"="+d+"正確"); 202 m++; 203 } 204 if(d!=f) 205 { 206 System.out.print(Repeat[w]+"="+d+"不正確"); 207 } 208 } 209 } 210 if(flag==1) 211 { 212 c6++; 213 } 214 215 } 216 System.out.println("共"+c6+"道題"+" "+m+"道題正確"); 217 } 218 if(c==2) 219 { 220 221 System.out.println("請輸入選擇:1 、有乘除法 2、無乘除法"); 222 Scanner input1=new Scanner(System.in) ; 223 c1=input1.nextInt(); 224 System.out.println("請輸入數值范圍的前域 "); 225 Scanner input2=new Scanner(System.in) ; 226 c2=input2.nextInt(); 227 System.out.println("請輸入數值范圍的后域"); 228 Scanner input3=new Scanner(System.in) ; 229 c3=input3.nextInt(); 230 /* System.out.println("請輸入選擇:1、加減有負數 2、加減無負數"); 231 Scanner input4=new Scanner(System.in) ; 232 c4=input4.nextInt(); 233 System.out.println("請輸入選擇:1、除法有余數 2、除法無余數"); 234 Scanner input5=new Scanner(System.in) ; 235 c5=input5.nextInt();*/ 236 System.out.println("請輸入出題數量"); 237 Scanner input6=new Scanner(System.in) ; 238 c6=input6.nextInt(); 239 System.out.println("請輸入在一行中輸出幾列運算式?"); 240 Scanner input7=new Scanner(System.in) ; 241 c7=input7.nextInt(); 242 System.out.println("請輸入選擇: 1、有括號 2、無括號"); 243 Scanner input8=new Scanner(System.in) ; 244 c8=input8.nextInt(); 245 //有無乘除法 246 if(c1==1) 247 { 248 e=rand.nextInt(4); 249 } 250 if(c1==2) 251 { 252 e=rand.nextInt(2); 253 } 254 //數值范圍 255 a=rand.nextInt(c3)%(c3-c2+1)+c2; 256 b=rand.nextInt(c3)%(c3-c2+1)+c2; 257 String []Repeat=new String[2*c6]; 258 if(e==0) 259 { 260 s="+"; 261 g=a+b; 262 } 263 if(e==1) 264 { 265 s="-"; 266 g=a-b; 267 } 268 if(e==2) 269 { 270 s="*"; 271 g=a*b; 272 } 273 if(e==3) 274 { 275 if(b==0) 276 { 277 flag=1; 278 } 279 if(b!=0) 280 { 281 flag=0; 282 s="/"; 283 g=a/b; 284 } 285 } 286 //出題數量 287 for(int w=0;w<c6;w++) 288 { 289 int p;//0為在原來基礎上前面加數,1則是在后面加數 290 int ws;//幾位數的運算 291 ws=rand.nextInt(8); 292 Repeat[w]=a+s+b; 293 if(c8==1)//有括號 294 { 295 296 for(int w1=0;w1<ws;w1++) 297 { 298 d1=rand.nextInt(c3)%(c3-c2+1)+c2; 299 p=rand.nextInt(2); 300 if(c1==1) 301 { 302 e=rand.nextInt(4); 303 } 304 if(c1==2) 305 { 306 e=rand.nextInt(2); 307 } 308 if(e==0) 309 { 310 s="+"; 311 g=d1+g; 312 } 313 if(e==1) 314 { 315 s="-"; 316 if(p==0) 317 { 318 g=d1-g; 319 } 320 if(p==1) 321 { 322 g=g-d1; 323 } 324 } 325 if(e==2) 326 { 327 s="*"; 328 g=d1*g; 329 } 330 if(e==3) 331 { 332 if(b==0||g==0) 333 { 334 flag=1; 335 } 336 if(b!=0&&g!=0) 337 { 338 flag=0; 339 s="/"; 340 if(p==0) 341 { 342 g=d1/g; 343 } 344 if(p==1) 345 { 346 g=g/d1; 347 } 348 } 349 } 350 if(p==0) 351 { 352 Repeat[w]=d1+s+"("+Repeat[w]+")"; 353 } 354 if(p==1) 355 { 356 Repeat[w]="("+Repeat[w]+")"+s+d1; 357 } 358 } 359 } 360 if(c8==2)//無括號 361 { 362 int length=0; 363 364 String [][]Repeat1=new String[2*c6][100]; 365 Repeat1[w][0]=""+a; 366 Repeat1[w][1]=s; 367 Repeat1[w][2]=""+b; 368 for(int w1=0;w1<ws;w1++) 369 { 370 371 d1=rand.nextInt(c3)%(c3-c2+1)+c2; 372 p=rand.nextInt(2); 373 if(c1==1) 374 { 375 e=rand.nextInt(4); 376 } 377 if(c1==2) 378 { 379 e=rand.nextInt(2); 380 } 381 if(e==0) 382 { 383 s="+"; 384 385 } 386 if(e==1) 387 { 388 s="-"; 389 390 } 391 if(e==2) 392 { 393 s="*"; 394 395 } 396 if(e==3) 397 { 398 if(b==0) 399 { 400 flag=1; 401 } 402 if(b!=0) 403 { 404 flag=0; 405 s="/"; 406 407 } 408 } 409 410 if(p==0) 411 { 412 int sl=0;//實際長度 413 Repeat[w]=d1+s+Repeat[w]; 414 length=Repeat1[w].length; 415 for(int xx=0;xx<length;xx++) 416 { 417 String ss=""; 418 ss=Repeat1[w][xx]; 419 if(ss!=null&&!ss.equals("")) 420 { 421 sl++; 422 } 423 else 424 { 425 break; 426 } 427 } 428 429 for(int l=0;l<sl;l++) 430 { 431 Repeat1[w][l+2]=Repeat1[w][l]; 432 } 433 Repeat1[w][0]=""+d1; 434 Repeat1[w][1]=s; 435 } 436 if(p==1) 437 { 438 int sl=0; 439 Repeat[w]=Repeat[w]+s+d1; 440 length=Repeat1[w].length; 441 for(int xx=0;xx<length;xx++) 442 { 443 String ss=""; 444 ss=Repeat1[w][xx]; 445 if(ss!=null&&!ss.equals("")) 446 { 447 sl++; 448 } 449 else 450 { 451 break; 452 } 453 } 454 Repeat1[w][sl]=s; 455 Repeat1[w][sl+1]=""+d1; 456 } 457 } 458 //計算結果 459 int []ns=new int[10]; 460 int []nf=new int[9]; 461 int ll=(Repeat1[w].length); 462 int sl=0; 463 for(int xx=0;xx<ll;xx++) 464 { 465 String ss=""; 466 ss=Repeat1[w][xx]; 467 if(ss!=null&&!ss.equals("")) 468 { 469 sl++; 470 } 471 else 472 { 473 break; 474 } 475 } 476 for(int l1=0;l1<sl;l1++) 477 { 478 if(l1%2==0) 479 { 480 ns[l1/2]=Integer.parseInt(Repeat1[w][l1]); 481 } 482 if(l1%2!=0) 483 { 484 485 nf[l1/2]=Repeat1[w][l1].charAt(0); 486 } 487 } 488 489 if(c1==1) 490 { 491 492 } 493 if(c1==2) 494 { 495 g=ns[0]; 496 for(int l2=1;l2<(sl/2+1);l2++) 497 { 498 if(nf[l2-1]==43) 499 { 500 g=g+ns[l2]; 501 } 502 if(nf[l2-1]==45) 503 { 504 g=g-ns[l2]; 505 } 506 } 507 } 508 } 509 510 511 512 //判斷重復 513 int w3=w; 514 for(int w2=0;w2<w3;w2++) 515 { 516 if(Repeat[w].equals(Repeat[w2])) 517 { 518 flag =1; 519 } 520 else 521 {flag =0;} 522 } 523 //打印 524 if(flag==1) 525 { 526 c6++; 527 } 528 if(flag==0) 529 { 530 if((w+1)%c7==0) 531 { 532 System.out.println(Repeat[w]+"="+g+" "); 533 } 534 else 535 { 536 System.out.print(Repeat[w]+"="+g+" "); 537 } 538 } 539 } 540 } 541 if(c==3) 542 { 543 break; 544 } 545 546 } 547 } 548 549 }轉載于:https://www.cnblogs.com/CkmIT/p/6694834.html
總結
- 上一篇: 将用int型存储的时间转换成time时间
- 下一篇: Selenium2+python自动化5