而java循环中的数字猜测

时间:2016-03-05 02:49:37

标签: java loops

所以我一直在研究它并且它工作正常,唯一的问题是,在你猜出一个不在1到100之间的数字之后,它会在尝试不应该的时候进入下一个猜测。对你不利。我附上了一张输出应该是什么样的图片。

我需要一个' for'语句或布尔变量?

enter image description here

这是我的代码:

Random generator = new Random();

//import the scanner for prompt
Scanner input = new Scanner(System.in);

//integers for the secret number, input guess of the secret number, tries and limit to tries
int numberToGuess = 1 + generator.nextInt(100);
int numberOfTries = 1;
int limit = 10;
int guess;

//prompt user to enter a number to guess
System.out.println("You have 10 tries to guess a number between 1 and 100");

//start of while loop
while (numberOfTries <= 10) {
    System.out.print("Guess number " + numberOfTries + ": ");
    guess = input.nextInt();
    //if else statements (outputs)
    if (guess < numberToGuess)
        System.out.println("Your guess is too low. Try again.");
    else if (guess > 100)
        System.out.println("Guesses should be between 1 and 100.");
    else if (guess > numberToGuess)
        System.out.println("Too high. Try again.");
    else if (guess == numberToGuess) {
        System.out.println("Congratulations! You have correctly guess the number in " + numberOfTries + " tries");
        System.exit(0);
    } else {
        System.out.println("Sorry, you did not guess the guess the answer in 10 tries");
        System.out.println("The number was " + numberToGuess);

        //break to end while loop
        break;
    }
    numberOfTries++;

    // If statement after executing the while loop the output if user loses and answer to secret number
    if (numberOfTries > 10) {
        System.out.println("Sorry, you did not guess the guess the answer in 10 tries");
        System.out.println("The number was " + numberToGuess);
    }
}

4 个答案:

答案 0 :(得分:1)

不要增加numberOfTries ++;如果他们猜测范围之外的数字。将增量放在if语句

答案 1 :(得分:0)

尝试在主do-while循环中使用嵌套的while循环:

//start of while loop
while(numberOfTries <= 10){
    //get a valid guess
    do{
        boolean valid = true;
        System.out.print("Guess number " + numberOfTries + ": ");
        guess= input.nextInt();
        //if else statements (outputs)
        if (guess < numberToGuess){
             System.out.println("Your guess is too low. Try again.");
             valid = false;
        }
        else if (guess > 100){
            System.out.println("Guesses should be between 1 and 100.");
            valid = false;
        }
    while (!valid);

    //handle the valid guess

}

答案 2 :(得分:0)

我是这样做的。你告诉我你喜欢哪一个:

import java.util.Random;
import java.util.Scanner;

/**
 * Created by Michael
 * Creation date 3/4/2016.
 * @link https://stackoverflow.com/questions/35809392/while-loop-in-java-number-guess
 */
public class NumberGuess {

    public static final int MIN_VALUE = 1;
    public static final int MAX_VALUE = 100;
    public static final int MAX_TRIES = 10;

    public static void main(String[] args) {
        Random generator = new Random();
        Scanner input = new Scanner(System.in);
        String playAgain;
        do {
            int answer = MIN_VALUE + generator.nextInt(MAX_VALUE);
            System.out.println(String.format("You have %d tries to guess a number between %d and %d", MAX_TRIES, MIN_VALUE, MAX_VALUE));
            boolean correct = playGuessingGame(input, answer);
            if (!correct) {
                System.out.println(String.format("The number was %d", answer));
            }
            System.out.print("Play again? [Y/N]: ");
            playAgain = input.nextLine();
        } while ("Y".equalsIgnoreCase(playAgain));
    }

    public static boolean playGuessingGame(Scanner input, int answer) {
        boolean correct = false;
        int numTries = 0;
        do {
            System.out.print(String.format("Guess %d: ", ++numTries));
            int guess = Integer.parseInt(input.nextLine());
            if (guess < MIN_VALUE) {
                System.out.println(String.format("Guess %d is below minimum %d", guess, MIN_VALUE));
            } else if (guess > MAX_VALUE) {
                System.out.println(String.format("Guess %d is above maximum %d", guess, MAX_VALUE));
            } else if (guess < answer) {
                System.out.println(String.format("Guess %d is too low. Try again.", guess));
            } else if (guess > answer) {
                System.out.println(String.format("Guess %d is too high. Try again.", guess));
            } else {
                System.out.println("Woo hoo!  You got it right!");
                correct = true;
                break;
            }
        } while (numTries < MAX_TRIES);
        return correct;
    }
}

你和你的教授意识到失去这个游戏几乎是不可能的。正确?

一个二分策略说你总能在10次尝试中得到正确的答案。在挑战中使这个数字变小。

答案 3 :(得分:0)

增量应该在if statemnt:D

内完成