Author |
Message |
nelsonkkyy
|
Posted: Thu Oct 04, 2012 6:39 pm Post subject: Guessing game! need a new random number after the first round. |
|
|
code: |
import java.util.Scanner;
import java.util.Random;
public class Guess {
public static void main (String args[]){
boolean guess = true;
boolean reply = true;
while (reply == true){
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int ran_num = random.nextInt(100);
while (guess == true){
System.out.println("Enter a number 1-100");
int getnum = scanner.nextInt();
if (getnum > 100 || getnum < 1){
System.out.println("Invalid input.");
System.out.println(" ");
}
else if(getnum < ran_num ){
System.out.println("Too small");
System.out.println(" ");
}
else if(getnum > ran_num){
System.out.println("Too Large");
System.out.println(" ");
}
else if (getnum == ran_num){
System.out.println("You guessed it! The number is " + ran_num);
System.out.println(" ");
System.out.println("Do you want to keep playing?");
if (scanner.next().equalsIgnoreCase("Yes")){
reply = true;
}
else{
guess = false;
}
}
}
}
}
}
|
I keep playing till the second round and so on, the secret number still remains the same.
How do I generate a new one? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Oct 04, 2012 6:48 pm Post subject: RE:Guessing game! need a new random number after the first round. |
|
|
code: | Random random = new Random(); |
Try moving that outside the loop. |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Thu Oct 04, 2012 7:40 pm Post subject: Re: Guessing game! need a new random number after the first round. |
|
|
code: |
public class Guess {
public static void main (String args[]){
boolean guess = true;
boolean reply = true;
Random random = new Random();
while (reply == true){
|
Do you mean by that?
I tried it and it still doesn't work. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Oct 04, 2012 7:55 pm Post subject: RE:Guessing game! need a new random number after the first round. |
|
|
Oops, my bad.
code: | int ran_num = random.nextInt(100); |
Try moving that line into the 2nd loop. |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Thu Oct 04, 2012 7:58 pm Post subject: Re: Guessing game! need a new random number after the first round. |
|
|
But that will changes the secret number every time i make a guess |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Oct 04, 2012 8:15 pm Post subject: RE:Guessing game! need a new random number after the first round. |
|
|
When is 'reply' ever false? |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Thu Oct 04, 2012 8:22 pm Post subject: Re: Guessing game! need a new random number after the first round. |
|
|
But even if i change the guess == false to replay == false, it wont make any different |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Oct 04, 2012 8:33 pm Post subject: RE:Guessing game! need a new random number after the first round. |
|
|
Oh, I found the actual problem at last.
code: | if (scanner.next().equalsIgnoreCase("Yes")){
reply = true;
}
else{
guess = false;
} |
Have a good look at that code, and tell me when your loops exit. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Thu Oct 04, 2012 8:44 pm Post subject: Re: Guessing game! need a new random number after the first round. |
|
|
i don't see anything there
so when i type yes to replay the game and if i type no for the (guess = false / reply = false) it stops? |
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Thu Oct 04, 2012 10:15 pm Post subject: RE:Guessing game! need a new random number after the first round. |
|
|
code: |
program
do replay game loop
make random value
contine choose loop = true
replay game = false
do continue choose loop
get choice
was choice to small?
if yes then
say to small
else was to large?
say to large
else
you guessed it
continue choose is false
do you want to play again?
replay game true?
else
replay game false!
end if
end if
while continue choose is true
while replay game equal true
print out "END OF GAME"
end program
|
|
|
|
|
|
![](images/spacer.gif) |
|