Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Guessing game! need a new random number after the first round.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nelsonkkyy




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
nelsonkkyy




PostPosted: 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.
Insectoid




PostPosted: 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.
nelsonkkyy




PostPosted: 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
Insectoid




PostPosted: 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?
nelsonkkyy




PostPosted: 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
Insectoid




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
nelsonkkyy




PostPosted: 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 Sad
so when i type yes to replay the game and if i type no for the (guess = false / reply = false) it stops?
TokenHerbz




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: