Method 1.
public static void Main (string[] args) {
System.out.println ("will start 10 addition tests");
Scanner scanner=new Scanner (system.in);
int count=0;
for (int i=1;i<=10;i++) {
int a= (int) (Math.random () *1000) +1;
int b= (int) (Math.random () *1000) +1;
System.out.println ("(" +i+ ")." +a+ "+" +b+ "=?");
System.out.println ("Please enter answer input-1 exit:");
int Resullt=scanner.nextint ();
if (resullt==-1) {
Return
}
if ((a+b) ==resullt) {
count++;
SYSTEM.OUT.PRINTLN ("correct");
}else{
SYSTEM.OUT.PRINTLN ("error");
}
}
SYSTEM.OUT.PRINTLN ("This test ends your score for" +count*10);
Method Two.
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println ("will start 10 addition tests ...");
Scanner scanner=new Scanner (system.in);
Used to record fractions
int score=0;
Build 10 Loops
for (int i=1;i<=10;i++) {
Randomly generate two X addend
int a= (int) (Math.random () *100);
int b= (int) (Math.random () *100);
int result=a+b;
Output an addition expression that needs to be computed
System.out.println ("(" +i+ ")." +a+ "+" +b+ "=?");
System.out.println ("Please enter the answer (input-1 exit):");
int Answer=scanner.nextint ();
To judge right and wrong
if (answer = =-1) {
Break
}else if (answer! = result) {
System.out.println ("error!");
Continue
}else{
score+=10;
System.out.println ("correct!");
Continue
}
}
Scanner.close ();
System.out.println ("At the end of this quiz, your score is:" +score);
Thinking: 1.return directly back to the bottom of the program is no longer executed, break terminates the current loop, the bottom of the program will still execute, so the first program has a bug.
The use of 2.continue.
3. Use of variable names.
Java Random output 10 calculation question