random number generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cdm2883
    New Member
    • Sep 2007
    • 53

    random number generator

    Ok im trying to make a lil random number game. I have most of the code and comments on how i want the game to work. but i can not get to it work. if you have any answers any help what so ever it woul be greatly appreciated!

    [code=java]
    package mooredMod2;

    import java.util.Scann er;
    import java.util.Rando m;

    public class GuessingGame {


    public static void main(String[] args) {


    Scanner scan = new Scanner(System. in);

    // declare a boolean variable named more and set it equal to true
    boolean more = true;

    Random generator = new Random();
    while (true)
    {
    // declare an integer variable named numGuesses and
    // set it equal to zero

    int numGuesses = 0;

    // declare a boolean name correct and set it equal to false

    boolean correct = false;

    // declare an integer variable named theirGuess

    int theirGuess;

    // generate a number between one and ten and store it in a
    // integer variable named value (see p126)

    int value;
    value = generator.nextI nt(10) + 1;
    System.out.prin tln ( + value );

    // print out the computer's value (for debugging purposes only) -
    // you would remove this before
    // really running the program but leave it in when you
    // turn your project in

    System.out.prin tln("Guess a number between 1 and 10");
    // read their input from the keyboard using the Scanner
    // class and store it in the variable theirGuess

    theirGuess = scan.nextInt ();


    while (value <= 0 ) // a loop from chapter 5
    {
    // increment the numGuesses variable
    numGuesses = value + 1;



    if(theirGuess<v alue)
    {
    System.out.prin tln("Too low!");

    }
    else if (theirGuess>val ue)
    {
    System.out.prin tln("Too high!");

    }



    //print out that they were correct and



    }
    // tell them how many guesses it took

    // set the variable correct to true

    // ask them if they want to play again - // let them know they need to answer true or false


    // read their input from the keyboard using the
    // Scanner class and store their answer in the variable named more
    scan.nextInt ();
    }
    }//end of main method


    }[/code]

    Thanks
    Last edited by Nepomuk; Sep 21 '08, 09:08 PM. Reason: Please use [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi cdm2883!
    First of all, please use the [CODE] ... [/CODE] tags - this is at least the second time I've added them for you. They are there for a good reason, you know?

    Next, what part of the code isn't working? You can't expect people to go through all of your code if they don't even know, what they're looking for. We're all willing to help you, but there are limits.

    Greetings,
    Nepomuk (Moderator)

    Comment

    • cdm2883
      New Member
      • Sep 2007
      • 53

      #3
      The partofthe code that i am trying to get working now is where it is suppose to print it the number is to high or to low or if it is coorect

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by cdm2883
        The part of the code that i am trying to get working now is where it is suppose to print it the number is to high or to low or if it is coorect
        OK, you should normally also post what it is (or isn't) doing, but I found it this time: Check that loop while(value <= 0) - is the condition ever true? And if so, will it always be true? If not, why isn't it true? (Just in case you didn't know, "a <= b" means "a is smaller than b or a equals b".)

        Greetings,
        Nepomuk

        Comment

        • cdm2883
          New Member
          • Sep 2007
          • 53

          #5
          thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

          [code=java]
          System.out.prin tln("Would you like to play again? (Eneter true or false)");

          while (more = true) ;
          {
          }[/code]

          I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


          Thanks
          Last edited by Nepomuk; Sep 22 '08, 12:48 PM. Reason: Please use [CODE] tags

          Comment

          • jx2
            New Member
            • Feb 2007
            • 228

            #6
            Originally posted by cdm2883
            thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

            [code=java]
            System.out.prin tln("Would you like to play again? (Eneter true or false)");

            while (more = true) ;
            {
            }[/code]

            I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


            Thanks
            i dont knoe anythink about your program but i see big problem in your last post
            Code:
            while (more = true) ;
            {
            }
            //there are at least two mistakes in one line the  "more=true" is always true
            // i think you meant " more==true" <--- notice double "="  
            //second the semicolon at the end of first line "{}" will be ignored !!
            
            //correct while loop will look something like that:
            
            while(more == true){
                  //source code here
            }

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by cdm2883
              thank you for helping me find that error that i did not catch! now i am trying to figure out how to write the code for when they are correct, and would like to play again. this is whati have so far...

              [code=java]
              System.out.prin tln("Would you like to play again? (Eneter true or false)");

              while (more = true) ;
              {
              }[/code]

              I think i need to use the if and else statments but i dont know what code to write if they say true to ask them to enter a number, and if they put false how to terminate the program


              Thanks
              Actually, that won't even work with the corrections by jx2. What you want will be something like this:
              [code=java]boolean more;
              do {
              more = false;
              //... the code you already have here
              System.out.prin tln("Would you like to play again? (Enter true or false)");
              String answer = ...; // Read the answer
              // If the answer is "true", set the boolean "more" to true, else to false
              } while(more)[/code] For reading the answer, have a look at the Scanner class and System.in.

              Greetings,
              Nepomuk

              Comment

              • cdm2883
                New Member
                • Sep 2007
                • 53

                #8
                ok so i have added this code...

                scan.next();
                do {
                more = true;
                if (theirGuess == value)
                {
                System.out.prin tln("Would you like to play again? (Eneter true or false)");
                }

                }while (more);

                {
                System.out.prin tln("Guess a number between 1 and 10");


                but now my program will skip code diffrent parts of it, i dont know how else to explain it. If you tell me what you need to look at i will post it.

                Thanks

                Comment

                • cdm2883
                  New Member
                  • Sep 2007
                  • 53

                  #9
                  and is that part of the code going to do what i want it to?


                  Thanks

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by cdm2883
                    ok so i have added this code...

                    scan.next();
                    do {
                    more = true;
                    if (theirGuess == value)
                    {
                    System.out.prin tln("Would you like to play again? (Eneter true or false)");
                    }

                    }while (more);

                    {
                    System.out.prin tln("Guess a number between 1 and 10");


                    but now my program will skip code diffrent parts of it, i dont know how else to explain it. If you tell me what you need to look at i will post it.

                    Thanks
                    Pay special attention to your datatypes. If you are reading the input as strings then you should use the equals method to compare the strings rather than ==.

                    Comment

                    Working...