Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

first year project

  • 17-02-2006 11:59AM
    #1
    Registered Users, Registered Users 2 Posts: 448 ✭✭


    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, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    There's no input being done in the loop!


  • Closed Accounts Posts: 5,058 ✭✭✭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, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


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


Advertisement