Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

first year project

Options
  • 17-02-2006 11:59am
    #1
    Registered Users Posts: 447 ✭✭


    im doing a number guess game as part of my course. theres a few problems with it. the loop doesnt seem to be working properly and the statement is printed 4 times instead of once. any help?


    import java.io.*;


    class guessAdvanced

    {

    public static void main (String[] args) throws IOException

    {
    BufferedReader stdin =
    new BufferedReader ( new InputStreamReader( System.in ) );

    String InData;


    int guess, count, limit;
    final double answer = 56;

    limit = 5;




    System.out.println("Enter your guess (1-100) but remember you only have 5 guesses:"); InData = stdin.readLine();
    guess = Integer.parseInt (InData);

    count = 1;

    while ( count < limit)
    {
    if ( guess < answer )
    {
    System.out.println("The answer is higher!"); }
    else if ( guess >= answer )
    {
    System.out.println("The answer is lower!"); }
    count = count +1; }
    if ( guess == answer)
    {
    System.out.println("Congragulations you have guessed correctly!!!! Well done");
    }
    else if ( guess != answer)
    System.out.println("Try again");
    }

    }


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    There's no input being done in the loop!


  • Closed Accounts Posts: 5,064 ✭✭✭Gurgle


    [b]reformatted[/b]
    import java.io.*;
    
    class guessAdvanced
    
    {
    
    public static void main (String[] args) throws IOException
    
    	{
    	BufferedReader stdin =
    	new BufferedReader ( new InputStreamReader( System.in ) );
    
    	String InData;
    
    	int guess, count, limit;
    	final double answer = 56;
    
    	limit = 5;
    
    	System.out.println("Enter your guess (1-100) but remember you only have 5 guesses:"); 
    	InData = stdin.readLine();
    	guess = Integer.parseInt (InData);
    
    	count = 1;
    
    	while ( count < limit) 
    		{
    		if ( guess < answer )
    			{
    			System.out.println("The answer is higher!"); 
    			}
    		else if ( guess >= answer )
    			{
    			System.out.println("The answer is lower!"); 
    			}
    		count = count +1; 
    		}
    
    	if ( guess == answer)
    		{
    		System.out.println("Congragulations you have guessed correctly!!!! Well done");
    		}
    	else if ( guess != answer) System.out.println("Try again");
    	}
    }
    
    Here is your code reformatted so you can see whats going on.
    Your InData and if ( guess == answer) instructions are outside your count loop.

    (thx Hobbes, didn't know about code tags)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    use the /i]code[i tags. will look even nicer reformatted.


Advertisement