Guessing game in java using math.random

后端 未结 1 353
灰色年华
灰色年华 2021-01-29 11:41

Hi I\'m trying to use Math.random to generate a random number between 0 and 100, then ask a user to enter a number between 0 and 100, or -1 to quit. If the number is out of boun

相关标签:
1条回答
  • 2021-01-29 12:10

    For a start, your call to keyboard.nextInt() (and its corresponding println of "Guess a number between 0-100") should be within your while loop.

    Then you should consider changing your while loop to

    while (true) {
        // read input from user
        if (guess < value) { // tell user their guess is too low
        } else if (guess > value) { // tell user their guess is too high
        } else { // tell user congrats, and call break to exit the while loop }
        }
    }
    

    Once you get that right, you can work on the nice-to-haves, like checking numbers guessed are within bounds, keeping track of how many guesses they've done, etc

    0 讨论(0)
提交回复
热议问题