Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Game Program HELPPPP

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Game Program HELPPPP

    hi i am an amatuer programming student
    i have to create a game in which the user guesses a number that the computer has already chosen, after every guess the computer must state whether the number the user chose needs to be higher or lower in order to get the answer right.
    my problem i can get the user to enter a number and the computer does state whether the number needs to be higher or lower it just wont ask for another guess of the number...
    how do i do this?
    my code looks like this
     int number;
                        c.print("You have chosen Game 1");
                        c.print("\nThis game is very simple, the computer will have chosen a number, "); 
                        c.print("\nyou must figure out this number. "); 
                        c.print("\nEverytime you enter a number the computer will tell you if you must go higher or lower."); 
                        Random randomGenerator = new Random();
                        for (int idx = 1;idx < 2; idx++) 
                        {
                        int randomInt = randomGenerator.nextInt(20) + 1;
                        c.print("\n");
                        c.print("The computer has chosen a number please enter your first guess.");
                        c.print("\n");
                        number = c.readInt();                          
                        if (number < randomInt)
                        { 
                            c.print("The number you have guessed is too low, guess again");
                        }
                        c.print("\n");
                        if (number > randomInt)
                        {  
                            c.print("The number you have guessed is too high, guess again");
                        } 
                        if (number == randomInt)
                        {
                            c.print("Congrats! You found the correct number! You Win");
                        }
                        }


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Game Program HELPPPP

    Try using a while loop instead of a for loop and change the position of your loop.
    Also in your if statements have the user enter input.
    It would look something like this
    //your code
    int randomInt = randomGenerator.nextInt(20) + 1;
    c.print("\n");
    c.print("The computer has chosen a number please enter your first guess.");
    c.print("\n");
    number = c.readInt(); 
    while (randomInt != number) {
    if (number < randomInt)
    {
    c.print("The number you have guessed is too low, guess again");
    //your user input method here
    }
    c.print("\n");
    if (number > randomInt)
    {
    c.print("The number you have guessed is too high, guess again");
    //your user input method here
    } 
    }
    if (number == randomInt)
    {
    c.print("Congrats! You found the correct number! You Win");
    }

    Also when you post code, use proper code tags
    Here is a link: BBCode - Wikipedia, the free encyclopedia
    Last edited by Shivam24; July 19th, 2011 at 05:57 PM.

  3. The Following User Says Thank You to Shivam24 For This Useful Post:

    nitwit3 (July 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    THANK U SOOO MUCHHH!!

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Game Program HELPPPP

    ur welcome : )

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    okai so i believe i did what u said.. if i didnt .. sorry
    im an amatuer.. but il show u what i did :
    {
    c.clear();
    int number;
    c.print("You have chosen Game 1");
    c.print("\nThis game is very simple, the computer will have chosen a number, ");
    c.print("\nyou must figure out this number. ");
    c.print("\nEverytime you enter a number the computer will tell you if you must go higher or lower.");
    Random randomGenerator = new Random();
    for (int idx = 1;idx < 2; idx++)
    {
    int randomInt = randomGenerator.nextInt(20) + 1;
    c.print("\n");
    c.print("The computer has chosen a number please enter your first guess.");
    c.print("\n");
    number = c.readInt();
    while (randomInt != randomInt) {
    if (number < randomInt)
    {
    c.print("The number you have guessed is too low, guess again");
    number = c.readInt();
    }
    c.print("\n");
    if (number > randomInt)
    {
    c.print("The number you have guessed is too high, guess again");
    number = c.readInt();
    }
    if (number == randomInt)
    {
    c.print("Congrats! You found the correct number! You Win");
    }
    }
    }
    }
    this still isnt doing the trick.. unless this isnt wat u told me to do ..

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Game Program HELPPPP

    Ok i see. I'm sorry i forgot to add a bracket so the while loop should look like this
    while (randomInt != randomInt) {
    if (number < randomInt)
    {
    c.print("The number you have guessed is too low, guess again");
    number = c.readInt();
    }
    c.print("\n");
    if (number > randomInt)
    {
    c.print("The number you have guessed is too high, guess again");
    number = c.readInt();
    } 
    }//This is where you should add the bracket to end the while loop.
    Also please post your code using code tags. Without them your code is shifted to the left making it hard to read

  8. The Following User Says Thank You to Shivam24 For This Useful Post:

    nitwit3 (July 19th, 2011)

  9. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    okay so i added the braket and now when i run this
    itl ask the user to enter a number but then it will stop.. it wont tell the user if its number is low or high ... it just stops....
    and sorry i just joined this website today .. im just getting used to it
    {
    c.clear();
    int number;
    c.print("You have chosen Game 1");
    c.print("\nThis game is very simple, the computer will have chosen a number, ");
    c.print("\nyou must figure out this number. ");
    c.print("\nEverytime you enter a number the computer will tell you if you must go higher or lower.");
    Random randomGenerator = new Random();
    for (int idx = 1;idx < 2; idx++)
    {
    int randomInt = randomGenerator.nextInt(20) + 1;
    c.print("\n");
    c.print("The computer has chosen a number please enter your first guess.");
    c.print("\n");
    number = c.readInt();
    while (randomInt != randomInt) {
    if (number < randomInt)
    {
    c.print("The number you have guessed is too low, guess again");
    number = c.readInt();
    }
    c.print("\n");
    if (number > randomInt)
    {
    c.print("The number you have guessed is too high, guess again");
    number = c.readInt();
    }
    }
    if (number == randomInt)
    {
    c.print("Congrats! You found the correct number! You Win");
    }
    }
    }

  10. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    HTML Code:
     {
                        c.clear();
                        int number;
                        c.print("You have chosen Game 1");
                        c.print("\nThis game is very simple, the computer will have chosen a number, "); 
                        c.print("\nyou must figure out this number. "); 
                        c.print("\nEverytime you enter a number the computer will tell you if you must go higher or lower."); 
                        Random randomGenerator = new Random();
                        for (int idx = 1;idx < 2; idx++) 
                        {
                        int randomInt = randomGenerator.nextInt(20) + 1;
                        c.print("\n");
                        c.print("The computer has chosen a number please enter your first guess.");
                        c.print("\n");
                        number = c.readInt();                          
                        while (randomInt != randomInt) {
                        if (number < randomInt)
                        { 
                            c.print("The number you have guessed is too low, guess again");
                            number = c.readInt();
                        }
                        c.print("\n");
                        if (number > randomInt)
                        {  
                            c.print("The number you have guessed is too high, guess again");
                            number = c.readInt();
                        } 
                        }
                        if (number == randomInt)
                        {
                            c.print("Congrats! You found the correct number! You Win");
                        }
                        }
                        }

  11. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Game Program HELPPPP

    Im sorry again.
    I messed up and posted "while (randomInt != randomInt)"
    it should be "while (number != randomInt)"
    "!=" means does not equal and that's why your program stops. a variable can never not equal itself

  12. The Following User Says Thank You to Shivam24 For This Useful Post:

    nitwit3 (July 19th, 2011)

  13. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    PERFECTTT!!!! THANK U SOOO MUCH!!!
    sorry if i was sorta hard to deal with .. i just started java a couple weeks ago
    thank u!!!!

  14. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Game Program HELPPPP

    Another example of being spoonfed code and trying to use it without understanding it.
    Improving the world one idiot at a time!

  15. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Game Program HELPPPP

    im very sorry

  16. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    i know .. im just a amatear why dont u explain it to me.. junky?
    thank u

  17. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Game Program HELPPPP

    What do you want me to explain?
    Improving the world one idiot at a time!

  18. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    im just confused about .. how do u know which loops to use? like .. when would u use each..
    and what can i do to understand them better

  19. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Game Program HELPPPP

    I use the general rule that if you know exactly how many times a loop should iterate use a for loop. When the number of iterations are unknown then use a while loop.

    Print 10 numbers - for loop
    Get user input until they type "exit" - while loop
    Improving the world one idiot at a time!

  20. The Following User Says Thank You to Junky For This Useful Post:

    nitwit3 (July 19th, 2011)

  21. #17
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    and when do u use the "do ... while" loop?

  22. #18
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Game Program HELPPPP


  23. The Following User Says Thank You to dlorde For This Useful Post:

    nitwit3 (July 19th, 2011)

  24. #19
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game Program HELPPPP

    Thank u
    and sorry to junky i know i was basically spoonfed that code
    that was my first thread post and i needed an urgent reply
    next time il be sure to ask more questions and understanding wat help im given
    (Y)

  25. #20
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Game Program HELPPPP

    Your best bet is to write small programs using each loop. Try to read a file. You will soon see which loops work best for a given situation.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  26. The Following User Says Thank You to JavaPF For This Useful Post:

    nitwit3 (July 20th, 2011)

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  2. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 07:01 PM
  3. Breakout Game- program help
    By strength.honor in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2010, 02:44 PM
  4. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 03:31 AM
  5. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM