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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

first year project

  • 17-02-2006 10:59am
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


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


Advertisement