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
  • 08-02-2006 5:19pm
    #1
    Registered Users Posts: 447 ✭✭


    im in first year and i cant manage to find the problem in the programme i have written. the basic concept is to write a programme to allow the user to play a number guessing game.

    import java.io.*;

    class guess

    {
    public static void main (String[] args) throws IOException
    {
    BufferedReader stdin =
    new BufferedReader ( new InputStreamReader( System.in ) );

    String InData;

    int guess;
    final double answer = 61;


    System.out.println("Enter your guess (1-100):");
    InData = stdin.readLine();
    guess = Integer.parseInt (InData);

    if ( guess = 61 )

    {
    System.out.println("Congragulations you have guessed correctly!");
    }
    else if ( guess != 61 )
    {

    System.out.println("You have not guessed correctly!");
    }
    if ( guess < 61 )
    {

    System.out.println("The answer is higher!");
    }
    else if ( guess >= 61 )
    {

    System.out.println("The answer is lower!");

    }
    }




    if anyone could help me find the problem it would be much appreciated

    its saying that a boolean is required on the first line of the if statement


Comments

  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    if ( guess = 61 ) .... have a good look at this line.

    PS: put constants on the left hand side if at all possible to avoid this error.


  • Registered Users Posts: 919 ✭✭✭timeout


    Take a good look at the line
    if ( guess = 61 )

    Also change the name of your class (guess) or the integer value (guess) to avoid confussion, I had to look at it twice.


  • Registered Users Posts: 447 ✭✭blocparty


    what are constants? have you spotted the mistake? and trynig to make me spot it?


  • Registered Users Posts: 4,188 ✭✭✭pH


    They're trying to say in java there's a difference between = (assignment) and == (equality). You should be using ==


  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    61 is a constant for example.

    I have spotted a mistake and am trying to help you spot it rather than give you the answer straight out.

    Hint: Change it to if(61 = guess)


  • Advertisement
  • Registered Users Posts: 447 ✭✭blocparty


    ok problem solved. thanks a million guys and thanks for the little pointers.


    now one final question:

    how do i get the programme to run itself allowing the user to enter a guess one after another without having to type in: java guessGame each time after a guess?

    ps i changed the name of the class to guessGame


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Stick in a loop (like a while loop) and let it keep looping until the press 'q' (for quit) or whatever.

    In the loop you should check for their next guess etc etc


  • Registered Users Posts: 447 ✭✭blocparty


    In the loop you should check for their next guess etc etc

    what do you mean by check their next guess?


  • Registered Users Posts: 919 ✭✭✭timeout


    No need to have that press q to quit thing just let the program run till you guess the right answer, much more fun :D

    He means that you need to check for the next guess they make if they don't get it right. Also if you do decide to put the press q to quit thing in you will then have to be checking for both characters (i.e the q) and integer( i.e. the guess)


  • Registered Users Posts: 4,188 ✭✭✭pH


    It's also a great chance to learn to use java classes and methods, how to structure a program and why you don't just stick all your code in the main method :)


  • Advertisement
  • Registered Users Posts: 447 ✭✭blocparty


    well programme is now complete managed to get the loop working myself so im happ with that. i just put in a line: while ( guess != answer)

    now i have to write an advanced version of the game. limited to 5 guesses.

    id like to put in lines so if the user enters a number outside 1 and 100 it will correct them and say please enter a number between 1 and 100. and another line that will make sure they dont guess the same number twice.

    but this will prob just confuse the whole thing


  • Registered Users Posts: 11,393 ✭✭✭✭Vegeta


    to limit the program to only allow the user to guess 5 times you will need something to count the number of guesses, so everytime a user enters a value make count 1 bigger,count should be initialised to zero everytime the program begins from the start.

    Sounds like you'll have to use a nested statement if you know what that is. if count is less than 5 and and then if guess != answer.


  • Registered Users Posts: 447 ✭✭blocparty


    ya im in the process of doing that now


  • Registered Users Posts: 447 ✭✭blocparty


    it says that the variable count might not have been initialized.

    not sure whats wrong it looks right to me


    while ( count <= limit)

    {
    count = 0;


    if ( guess < 57 )
    {
    System.out.println("The answer is higher!");
    }

    else if ( guess >= 57 )

    {
    System.out.println("The answer is lower!");
    }


    count = count +1;


  • Registered Users Posts: 4,188 ✭✭✭pH


    You need to initialise count to zero before the while
    int count = 0;
    while (count++ <= limit) {
     ...
    }
    
    or use a for
    for (int count=0;count<= limit; count++) {
     ...
    }
    


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


    try initialising the count variable outside of the loop, and use count++ instead of count = count + 1. Your missing a closing brace for the loop as well.


  • Registered Users Posts: 447 ✭✭blocparty


    ya forgot that you had to initialize outside the loop. i have a closing brace just didnt copy and paste it by mistake.

    nice one lads


  • Closed Accounts Posts: 888 ✭✭✭themole


    you should also not us the number 62 directly,

    you should be using answer everywhere 61 is used so that to change the answer you only have to change the value of the answer variable.

    ie
    if ( guess < 61 )
    {

    System.out.println("The answer is higher!");
    }

    should be
    if ( guess < answer )
    {

    System.out.println("The answer is higher!");
    }


  • Registered Users Posts: 919 ✭✭✭timeout


    Well to make it a proper guessing game, you should randimise the answer. It will pick a random number between 1-100 automatically when the program starts. But that is a minor detail to be added when the program is working for a set value.


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


    pH wrote:
    You need to initialise count to zero before the while

    A gotcha for future reference. If the variable is not within a method it is automatically initialised to its default value.

    eg.
    class MyClass {
    
     int count;  // This will default to zero.
    
     myMethod() { 
       int secondCount; // This will not compile. Needs a value. 
       int thirdCount = 0; // This is what you should do here. 
     }
    
    } 
    


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Its good practice to initialise all your values. It can help stop stupid/hard-to-find bugs from cropping up later on. Program in C and you'll know what i mean.


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


    Its good practice to initialise all your values. It can help stop stupid/hard-to-find bugs from cropping up later on. Program in C and you'll know what i mean.

    True. Although the compiler will give a warning that a variable might not be initialised before being used.


  • Registered Users Posts: 3,182 ✭✭✭dionsiseire


    might be an idea to look up the Rand function

    randomise the answer and make it a bit more useful to yourself


Advertisement