Skip to main content

how to get this set of code right (while, if, else)

1 reply [Last post]
Joined: 2011-07-09

import java.util.Scanner; // Make the Scanner class available.

public class test
public static void main(String[] args)
int number1 = (int)(100 * Math.random()) + 1;
int number2 = (int)(100 * Math.random()) + 1;
int answer;

System.out.println ("This is a addition maths program");
System.out.println (number1 + "+" + number2);
Scanner stdin = new Scanner( ); // Create the Scanner.
System.out.println ("What's your answer?: ");
answer = stdin.nextInt(); //to react with the user input
//this is for the loop, as long as the input is incorrect
while (answer = None)
if (answer == (number1 + number2))
System.out.println ("You are correct");
System.out.println ("The sum is " + (number1 + number2));
System.out.println ("Your answer is incorrect, please try again");
hi, the above is the code i write. it's a simple math question. it should loop as long as the answer isn't correct.
can anyone point out the correct way?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-04-05

you need a loop around the question and answer !!! !!! :-)