View Full Version : I am new to java

03-28-2008, 02:45 AM
I am trying to write a program that helps people learn multiplication. I have the main program written and it works. Now I have to make it ask another question if the uses answers correctly or repeat the same question if the user answers wrongly. yes this is for a class, but I am just really lost and hope someone will help me.

import java.util.Scanner;

public class Multiplication {
public static void main (String[] args)

int number1 = 0;
int number2 = 0;
int actualAnswer = 0;
int userAnswer;

number1 = (int)(Math.random() * 9 +1);
number2 = (int)(Math.random() * 9 +1);
actualAnswer = number1 * number2;

// get answer from user
Scanner input = new Scanner (System.in);
System.out.println("How much is " + number1 + " TIMES " + number2 + "?");
userAnswer = input.nextInt();

// check answer
if ( userAnswer != actualAnswer ) // if answer is not correct
System.out.printf( "No. Please try again.");

if ( userAnswer == actualAnswer ) // if answer is correct
System.out.printf( "Very Good!");

} // end

03-28-2008, 03:41 AM
Look into a while or dowhile loop.

03-28-2008, 05:21 PM
Hey, in your if statment you could use a temporary variables to remember the current value, and if it wrong, set number1 and number2 to the value and then ask the sum again.

Depending on how long you want it to last you might want to put the entire thing in a while loop, so it loops until the person gets 10 right, or whatever.

I hope that makes sense!