Java random output 10 calculation problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.