
-----------------------------------
nelsonkkyy
Thu Oct 04, 2012 6:39 pm

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;
						}

					}

				}
			}
	     }

}
[/code]

I keep playing till the second round and so on, the secret number still remains the same.
How do I generate a new one?

-----------------------------------
Insectoid
Thu Oct 04, 2012 6:48 pm

RE:Guessing game! need a new random number after the first round.
-----------------------------------
[code]Random random = new Random(); [/code]

Try moving that outside the loop.

-----------------------------------
nelsonkkyy
Thu Oct 04, 2012 7:40 pm

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){

[/code]
Do you mean by that?
I tried it and it still doesn't work.

-----------------------------------
Insectoid
Thu Oct 04, 2012 7:55 pm

RE:Guessing game! need a new random number after the first round.
-----------------------------------
Oops, my bad.
[code]int ran_num = random.nextInt(100); [/code]

Try moving that line into the 2nd loop.

-----------------------------------
nelsonkkyy
Thu Oct 04, 2012 7:58 pm

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

-----------------------------------
Insectoid
Thu Oct 04, 2012 8:15 pm

RE:Guessing game! need a new random number after the first round.
-----------------------------------
When is 'reply' ever false?

-----------------------------------
nelsonkkyy
Thu Oct 04, 2012 8:22 pm

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

-----------------------------------
Insectoid
Thu Oct 04, 2012 8:33 pm

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; 
}[/code]

Have a good look at that code, and tell me when your loops exit.

-----------------------------------
nelsonkkyy
Thu Oct 04, 2012 8:44 pm

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?

-----------------------------------
TokenHerbz
Thu Oct 04, 2012 10:15 pm

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
[/code]
