0

Hi guys I have a little bug which needs an extra eye to see I cant seem to see it. Can you help. Thanks in advance

//CAI - Computer Assisted Instruction, multiplication program for first graders
//Instruction.java

import java.util.Random; //Program uses Random class
import java.util.Scanner;// Program uses Scanner class

public class Instruction
{
   //Main method to execute the java program
   public static void main (String[] args)
  {  
     //Scanner object to get input of the user
     Scanner input = new Scanner(System.in);

     generateRandom();//Calling generate number
     int answer = input.nextInt();

     if ( answer == generateRandom() )
     System.out.println("You are correct");

     else 
     System.out.println("You are incorrect");

  }//end method main

  public static int generateRandom()
  {
    Random randomNumbers = new Random();

    int digit1 = 1 + randomNumbers.nextInt(9);
    int digit2 = 1 + randomNumbers.nextInt(9);

    System.out.printf("How much is %d times %d", digit1, digit2);
    System.out.println();
    int result = digit1 * digit2;

    return result;
   }//end method generateRandom

}//end class Instruction
2
Contributors
2
Replies
20
Views
3 Years
Discussion Span
Last Post by Kevin_1
2
generateRandom();//Calling generate number
     int answer = input.nextInt();
     if ( answer == generateRandom() )

well, if you call it twice, it'll be executed twice.
replace the above by either:

 int answer = input.nextInt();
 if ( answer == generateRandom() )

or by

 int generated = generateRandom();//Calling generate number
 int answer = input.nextInt();
 if ( answer == generated )
0

Thanks Man It worked I was calling the method after getting the input. Thanks! `+stultuske

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.