猜猜游戏范围

时间:2015-02-14 12:11:54

标签: java

我已经完成了我的课程作业,除了一件无法弄清楚的事情。 如何让程序只允许1到100之间的数字?如果用户键入小于1或大于100的数字,则应告知用户该数字超出范围。

我设置了一个随机类和一个while循环,它接受输入并告诉用户数字太高或太低。如果数字正确,显示您赢得按摩。一切正常但只有范围部分。  import java.util.Scanner; import java.util.Random;

公共课GuessingGame {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    Random generator = new Random(); 
    int numberToGuess = 1 + generator.nextInt(100);
    int numberOfTries = 0;

    Scanner input = new Scanner (System.in);
    int guess;
    boolean win = false;


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

    while(!win && numberOfTries < 10)
    {
    System.out.println("Guess a number: ");
    guess = input.nextInt();

    if (guess < 1 || guess > 100)
    {
    System.out.println("Guess is out of range!");
    continue; 
    }


    if (guess == numberToGuess) 
    {
        win=true;
    }

    else if (guess < numberToGuess)
            {
            System.out.println("Your guess is too low");
            }
    else if (guess > numberToGuess)
            {
            System.out.println("Your guess is too high");
            }

    }

    System.out.println("You win!");
    System.out.println("The number was " + numberToGuess);
    System.out.println("It took you " + numberOfTries + " tries." );


}

} - 我添加了其余的代码 -

但如果我键入-1或101之类的数字,程序会说数字太高或数字太低,忽略了超出范围的声明。无法弄明白为什么。 请你帮助我好吗? 谢谢

3 个答案:

答案 0 :(得分:1)

我在您的代码中添加了一些改进:

检查有效范围后

numberOfTries++,因为你需要增加这个数字,否则循环不会在10次猜测后结束。

玩家获胜时

break,因为在这种情况下你不希望循环继续。

在循环之后你需要检查玩家是否赢了。他也有可能在没有获胜的情况下猜了10次。

我认为它应该按预期工作。

public static void main(String[] args) {
    Random generator = new Random(); 
    int numberToGuess = 1 + generator.nextInt(100);
    int numberOfTries = 0;

    Scanner input = new Scanner (System.in);
    int guess;
    boolean win = false;

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

    while(!win && numberOfTries < 10)
    {
        System.out.println("Guess a number: ");
        guess = input.nextInt();

        if (guess < 1 || guess > 100)
        {
            System.out.println("Guess is out of range!");
            continue; 
        }

        // increase numberOfTries by 1 because player made another valid guess
        numberOfTries++;

        if (guess == numberToGuess) 
        {
            win=true;
            // exit the while loop because player won
            break;
        }

        if (guess < numberToGuess)
        {
            System.out.println("Your guess is too low");
        }
        else 
        {
            // guess must be > numberToGuess here
            System.out.println("Your guess is too high");
        }
    }

    if(win)
    {
        System.out.println("You win!");
        System.out.println("The number was " + numberToGuess);
        System.out.println("It took you " + numberOfTries + " tries." );
    }
    else
    {
        System.out.println("Sorry, you didn't win.");
        System.out.println("The right number was " + numberToGuess);
    }
}

答案 1 :(得分:0)

好吧,break会使程序退出while循环,而不会再提供任何输入数字的机会。请改用continue

答案 2 :(得分:0)

break关键字用于退出最近的循环体,因为这是你的while循环,使用它将结束你的猜数游戏。您想使用continue关键字,它将立即开始循环的下一次迭代:

while(!win && numberOfGuesses < 10)
{
    System.out.println("Guess a number: ");
    guess = Integer.parseInt(input.nextLine());

    if (guess < 1 || guess > 100)
    {
        System.out.println("Guess is out of range!");
        continue; //Use this instead of break
    }

    //Rest of your code
}