Results 1 to 9 of 9
  1. #1
    lucan is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default how to get this set of code right (while, if, else)

    Java Code:
    import java.util.Scanner; // Make the Scanner class available.
    
    public class test
    {
    public static void main(String[] args)
    {
    int number1 = (int)(100 * Math.random()) + 1;
    int number2 = (int)(100 * Math.random()) + 1;
    int answer;
    
    System.out.println ("This is a addition maths program");
    System.out.println (number1 + "+" + number2);
    Scanner stdin = new Scanner( System.in ); // Create the Scanner.
    System.out.println ("What's your answer?: ");
    answer = stdin.nextInt(); //to react with the user input
    //this is for the loop, as long as the input is incorrect
    while (answer = None)
       if (answer == (number1 + number2))
          System.out.println ("You are correct");
          System.out.println ("The sum is " + (number1 + number2));
       else
          System.out.println ("Your answer is incorrect, please try again");
    }
    }
    hi, the above is the code i write. it's a simple math question. it should loop as long as the answer isn't correct.
    can anyone point out the correct way?
    thanks.
    Last edited by JosAH; 07-17-2011 at 11:39 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    I added code tags and fixed the indentation for you and noticed that your curly brackets are incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What exactly is the problem? Errors? If so, what are they?

    Also, a common practice for looping and getting input is to combine the test and the line reading in the while loop.

    Java Code:
    while((answer=stdIn.nextInt()) != correctAnswer){}
    This will keep getting input until they get a correct answer, and you can print a wrong answer message and re prompt after each iteration. Once it gets past the loop you know they got the correct answer. Try playing around with the above concept and see if if helps you.

  4. #4
    lucan is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    None cannot be resolved
    Syntax error on token "else", delete this token

    at test.main(test.java:17)
    the above is the error message i got. I'm using eclipse.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by lucan View Post
    the above is the error message i got. I'm using eclipse.
    You didn't define 'None' anywhere in your code; that 'else' thing happens because you didn't fix your curly brackets (see my first reply).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you use if and else you are allowed a single statement after the if. Statement blocks are considered one statement so if you want to have more than one semi colon terminated line after an if you need to wrap it in brackets(you should always do this to avoid the problem)
    Java Code:
    if(condition){
      //statements
    }
    else{
      //statements
    }

  7. #7
    lucan is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;  // Make the Scanner class available.
    
    public class test
    {
    	public static void main(String[] args) 
    	{
    		int number1 = (int)(100 * Math.random()) + 1;
    		int number2 = (int)(100 * Math.random()) + 1;
    		int answer;
    		int None;
    		
    		System.out.println ("This is a addition maths program");
    		System.out.println (number1 + "+" + number2);
    		Scanner stdin = new Scanner( System.in );  // Create the Scanner.
    		System.out.println ("What's your answer?: ");
    		answer = stdin.nextInt(); //to react with the user input
    		//this is for the loop, as long as the input is incorrect
    		while (answer == None)
    			if (answer == (number1 + number2))
    				System.out.println ("You are correct");
    				System.out.println ("The sum is " + (number1 + number2));
    			else
    				System.out.println ("Your answer is incorrect, please try again");
    	}
    }
    hi,
    i really don't know where i should place any more {}.
    thanks

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    See my post number 6. Also, that while loop isn't needed(not only because none isn't defined), but because you don't prompt for a new answer. Loops are meant to repeat some task x number of times.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Have a close look at this part of your code:

    Java Code:
    			if (answer == (number1 + number2))
    				System.out.println ("You are correct");
    				System.out.println ("The sum is " + (number1 + number2));
    			else
    				System.out.println ("Your answer is incorrect, please try again");
    The compiler doesn't care about indentation, nor does it attach any special meaning to it, only humans do; all that a compiler wants is proper grouping of statements. You do that with those curly braces. As it is now, your code is identical to the following:

    Java Code:
    			if (answer == (number1 + number2))
    				System.out.println ("You are correct");
    			System.out.println ("The sum is " + (number1 + number2));
    			else
    				System.out.println ("Your answer is incorrect, please try again");

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. look @ code and give ideas! Install new code....
    By turbowhat in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 04:08 AM
  2. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  3. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •