Results 1 to 2 of 2
  1. #1
    Mrlowrider is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default if statement help

    Well I have two quesions actually.

    PART 1

    What I am trying to get this program do is ask math three math questions. If the answer is right it will print out it is right and you will get one point or it will be wrong and get zero points. I have done that. However, when i try to declare a variable that is the total of the scores it says that the java program can't find the variables.
    Java Code:
    System.out.println("Enter an Integer:");
                 	int int1=keyboard.nextInt();
    	         System.out.println("Enter another Integer:");
    	         	int int2=keyboard.nextInt();
    	         	
    	          System.out.println("Answer the following questions please.");
    	          System.out.println("");
    	          System.out.println("1."+int1+"*"+int2+"=");
    	          	int guess1=keyboard.nextInt();
    	          			if (guess1==(int1*int2))
    	          			{System.out.println("Right!");
    	          				int score1= 1;
    	          			}
    	          			else 
    	          			{System.out.println("Wrong!");
    	          				int score1= 0;
    	          			}
    	        
    	          			System.out.println("2."+int1+"/"+int2+"=");
    	    	          	int guess2=keyboard.nextInt();
    	    	          			if (guess2==(int1/int2))
    	    	          			{System.out.println("Right!");
    	    	          				int score2= 1;
    	    	          			}
    	    	          			else
    	    	          			{System.out.println("Wrong!");
    	    	          				int score2= 0;
    	    	          			}
    	    	          	System.out.println("3."+int1+"%"+int2+"=");
    	    	    	          	int guess3=keyboard.nextInt();
    	    	    	          			if (guess3==(int1%int2))
    	    	    	          			{System.out.println("Right!");
    	    	    	          				int score3= 1;
    	    	    	          			}
    	    	    	          			else
    	    	    	          			{System.out.println("Wrong!");
    	    	    	          				int score3= 0;
    	    	    	          			}
    	    	          				
    	                  int total= score1+score2+score3;

    Part 2

    my second question is just what to set the if statement equal to so that it reads that the user has inputted nothing and just hit enter.

    Say for example

    if (stringOne= no input they just hit enter here)
    {System.out.println("you typed nothing");

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Question 1- You're declaring the variables for each score inside each if statement, so they're only available inside their respective if blocks. Declare them (or just one score variable that you increment) outside of the if block.

    Question 2- What have you tried? What is keyboard? Is it a Scanner? Check out the API and experiment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: 02-15-2011, 06:51 AM
  2. If Statement in SQL
    By Steffi1013 in forum JDBC
    Replies: 6
    Last Post: 04-10-2010, 04:19 PM
  3. need help in sql statement
    By chyeeqi in forum JDBC
    Replies: 6
    Last Post: 03-28-2010, 08:49 PM
  4. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 07:57 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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