PackageJava daily training;ImportJava.util.Random;ImportJava.util.Scanner; Public classPractice { Public Static voidMain (string[] args) {//TODO Auto-generated method stubs//control the number of questions intM,m1,m2,m3,m4,m5,s=0,k=0; System.out.print ("Please enter the number of questions:"); Scanner SC=NewScanner (system.in); M=Sc.nextint (); System.out.print ("Please enter a range of values (minimum):"); Scanner min=NewScanner (system.in); M1=Sc.nextint (); System.out.print ("Please enter a range of values (maximum number):"); Scanner Max=NewScanner (system.in); M2=Sc.nextint (); if(m1>=m2) {System.out.print ("Input Error! "); System.exit (0);//exit the entire program} System.out.print ("Is there a negative number (1 or No 2):");//determine if there are negative numbersScanner fs =NewScanner (system.in); M5=Sc.nextint (); if(m5==2){ if(m1>=m2| | m1<0| | M2<0) {System.out.print ("Input Error! "); System.exit (0);//exit the entire program}} Random R1=NewRandom (10); Random R2=NewRandom (20); Random R3=NewRandom (30); Random R4=NewRandom (40); Random R5=NewRandom (50); System.out.print ("Is there a multiplication method (1 or No 2):"); Scanner yn=NewScanner (system.in); M3=Sc.nextint (); System.out.print ("Division has no number (there are 1 No 2):");//control whether the surplus numberScanner remainder =NewScanner (system.in); M4=Sc.nextint (); for(inti = 0;i < m;i++){ intc = R3.nextint (120); if(m3==2) {k=c%2; } //control whether there is a multiplication method Elsek=c%4; intA = R1.nextint (m2-m1+1) +M1; intb = R2.nextint (m2-m1+1) +M1; Switch(k) { Case0:system.out.println (A + "+" +b+ "="); Break; Case1: if(m5==2){ if(a>b) {System.out.println (a+ "-" +b+ "="); Break; } ElseSystem.out.println (b + "-" +a+ "="); Break; } ElseSystem.out.println (A + "-" +b+ "="); Break; Case2:system.out.println (A + "*" +b+ "="); Break; Case3:if(m4==2){ if(a%b==0) {System.out.println (a+ "/" +b+ "="); Break; } ElseLabel: for(intq=1;q<10000000;q++)//To ensure that there is a certain number of divisions in the topic, it continues until a valid division is generated. { intf = R4.nextint (m2-m1+1) +M1; intD = R5.nextint (m2-m1+1) +M1; if(f%d==0) {System.out.println (f+ "/" +d+ "="); Breaklabel; } } } ElseSystem.out.println (A + "/" +b+ "="); Break; } } } }
Design ideas:
First, use a statement to customize the quantity and the way you print.
Then, the control parameters, whether there is a multiplication method (let the user choose, if it is, then add the multiplication method, if not, then delete the multiplication method), the value range (random Control), plus minus negative (1.random value does not take negative numbers,2, If it is a division, then add a control condition the first number is greater than the second number, the division has an empty number (in % to control, if there is no remainder, then add a condition, if the operator is division,a%b=0), whether to support fractions (separately add output function, judge the output of the score).
It should be judged whether the topic repeats. With a for loop control, if the first number repeats, then check the second number, if the second number is also repeated, then check the operation symbol, if all repeat, then delete this question, another problem. If one of these three controls is not satisfied, then break exits.
Results:
Programming sentiment: When processing the addition and subtraction of negative numbers, if there are negative numbers in the subtraction, then the subtraction of the two number of positions, this is more convenient.
Do not put the true score and avoid repeating the program, will be added later.
Too many variables in the program are too cumbersome. Should be a little more concise. No arrays are applied.
The initial program used too many for loops, causing the program to be too cumbersome, and then corrected, using only a for loop.
Time Record table
Time |
Unavailable |
|
3.14 |
2h |
Number and numerical range of topics |
3.15 |
2h |
Whether there is a negative number, whether there is multiplication |
3.16 |
1h |
Whether the multiplication is a surplus number |
3.17 |
1h |
Discovery Program too complicated |
3.18 |
2h |
Re-assemble, improve |
3.19 |
1h |
Find division too small to improve the number of divisions guaranteed |
Primary School Topics