# Game Program HELPPPP

• July 19th, 2011, 05:02 PM
nitwit3
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
Code Java:

``` 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"); } }```
• July 19th, 2011, 05:48 PM
Shivam24
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
Code :

```//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
• July 19th, 2011, 05:56 PM
nitwit3
Re: Game Program HELPPPP
THANK U SOOO MUCHHH!!
• July 19th, 2011, 05:57 PM
Shivam24
Re: Game Program HELPPPP
ur welcome : )
• July 19th, 2011, 06:05 PM
nitwit3
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("\n");
while (randomInt != randomInt) {
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");
}
}
}
}
this still isnt doing the trick.. unless this isnt wat u told me to do ..
• July 19th, 2011, 06:09 PM
Shivam24
Re: Game Program HELPPPP
Ok i see. I'm sorry i forgot to add a bracket so the while loop should look like this
Code :

```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.```
• July 19th, 2011, 06:15 PM
nitwit3
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
Quote:

{
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("\n");
while (randomInt != randomInt) {
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");
}
}
}
• July 19th, 2011, 06:17 PM
nitwit3
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");                     }                     }                     }```
• July 19th, 2011, 06:19 PM
Shivam24
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
• July 19th, 2011, 06:21 PM
nitwit3
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!!!!
• July 19th, 2011, 06:23 PM
Junky
Re: Game Program HELPPPP
Another example of being spoonfed code and trying to use it without understanding it.
• July 19th, 2011, 06:24 PM
Shivam24
Re: Game Program HELPPPP
im very sorry
• July 19th, 2011, 06:25 PM
nitwit3
Re: Game Program HELPPPP
i know :( .. im just a amatear why dont u explain it to me.. junky?
thank u
• July 19th, 2011, 06:26 PM
Junky
Re: Game Program HELPPPP
What do you want me to explain?
• July 19th, 2011, 06:27 PM
nitwit3
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
• July 19th, 2011, 06:31 PM
Junky
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
• July 19th, 2011, 06:33 PM
nitwit3
Re: Game Program HELPPPP
and when do u use the "do ... while" loop?
• July 19th, 2011, 06:33 PM
dlorde
Re: Game Program HELPPPP
• July 19th, 2011, 06:36 PM
nitwit3
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)
:)
• July 20th, 2011, 03:26 AM
JavaPF
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.