Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I am new to java

    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.


    Code:
    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
    }

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Look into a while or dowhile loop.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •