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

1. ## 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("\n");
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. ## 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("\n");
while (randomInt != number) {
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");
}```

Also when you post code, use proper code tags
Here is a link: BBCode - Wikipedia, the free encyclopedia

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

nitwit3 (July 19th, 2011)

4. ## Re: Game Program HELPPPP

THANK U SOOO MUCHHH!!

5. ## Re: Game Program HELPPPP

ur welcome : )

6. ## 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 ..

7. ## 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");
}
c.print("\n");
if (number > randomInt)
{
c.print("The number you have guessed is too high, guess again");
}
}//This is where you should add the bracket to end the while loop.```

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

nitwit3 (July 19th, 2011)

9. ## 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("\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");
}
}
}

10. ## 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("\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");
}
}
}```

11. ## 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. ## 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. ## Re: Game Program HELPPPP

Another example of being spoonfed code and trying to use it without understanding it.

15. ## Re: Game Program HELPPPP

im very sorry

16. ## Re: Game Program HELPPPP

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

17. ## Re: Game Program HELPPPP

What do you want me to explain?

18. ## 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. ## 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

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

nitwit3 (July 19th, 2011)

21. ## Re: Game Program HELPPPP

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

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

nitwit3 (July 19th, 2011)

24. ## 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. ## 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.

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

nitwit3 (July 20th, 2011)