#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    Java if statment help


    Hello,
    Well i have this test game and I was having problems with the if statement part. I says
    Incompatible operand types Scanner and int Game.java /Math
    Code:
    import java.util.Scanner; 
    import java.util.Random;
    public class Game {
    	public void game() {
    		// Question 1 
    		Random question1 = new Random();
    		int number1;
    		
    			number1 = question1.nextInt (10);
    			
    			Random question2 = new Random();
    			int number2;
    			
    				number2 = question2.nextInt (10);
    
    				int questionaanswerreal = number1 + number2;
    			
    				
    				System.out.println("Question 1: " + number1 + " + " + number2 + " = ?");
    				Scanner question1aansweruser = new Scanner(System.in);
    				if (question1aansweruser == questionaanswerreal){
    					System.out.println("Correct!");
    				}else{
    					System.out.println("Incorrect");
    				}
    	}
    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Incompatible operand types Scanner and int
    What do you want the code to compare in the if statement?
    Comparing a Scanner object to an int doesn't make any sense.

    Do you want the code to read in a number from the user and compare that?
    Look at the Scanner class's API doc for a method that will read a value from the user
    that can be saved in a variable in the program and then compared to another variable.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by NormR
    What do you want the code to compare in the if statement?
    Comparing a Scanner object to an int doesn't make any sense.

    Do you want the code to read in a number from the user and compare that?
    Look at the Scanner class's API doc for a method that will read a value from the user
    that can be saved in a variable in the program and then compared to another variable.
    Well what I am trying to do is check if the user inputted the correct answer IE: the variable questionaanserreal is the real answer I want it to see if the users answer = that.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Look at the Scanner class's API doc for a method that will read a value from the user
    that can be saved in a variable in the program and then compared to another variable.

IMN logo majestic logo threadwatch logo seochat tools logo