0

我真正坚持的是倒数第二个变量 userGuessDifference。它保持为零,使我的第二个 while 循环无法正常运行,因为它只是不断返回到第一个 else if 语句。

public class GuessingGame {

/**
 * @param args
 */
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
int userGuessDifference = (Math.abs(correctAnswer) - Math.abs(userGuess));
boolean flag = false;

System.out.println("We are going to play a number guessing game."); 
System.out.println(" ");

System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();

if (difficulty == 10)
{
    guesses = 3;
    System.out.println("You have 3 guesses, make them count!");
}   
else if (difficulty == 25)
{   
    guesses = 5;
    System.out.println("You have 5 guesses, make them count!");
}
else if (difficulty == 50) 
{
    guesses = 6;
    System.out.println("You have 6 guesses, make them count!");
}
else
{   
    System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
    difficulty = (difficulty * 100);
    guesses = 1;
}

System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();

while (!flag || (counter <= guesses))
{

    if (userGuess == correctAnswer)
    {
        System.out.println("CONGRATS YOU WIN!");
        flag = true;
    }

    else if ((userGuessDifference <= (difficulty * .10)))
    {
        System.out.println("HOT!");
        userGuess = keyboard.nextInt();
        counter++;
    }

    else if ((userGuessDifference < (difficulty * .25)) && (userGuessDifference > (difficulty * .10)))
    {
        System.out.println("Warm...");
        userGuess = keyboard.nextInt();
        counter++;
    }

    else
    {
        System.out.println("Ice cold.");
        userGuess = keyboard.nextInt();
        counter++;
    }

}
}
}
4

1 回答 1

1

正如@SotiriosDelimanolis 所写,您永远不会重新分配userGuessDifference。这应该在 while 循环内完成。

此外,您的代码还有另一个问题:如果您猜到数字,程序只会打印“恭喜您赢了!” 永远,但是在我看来,一旦用户猜到了数字,您就想退出 while 循环(我猜是出于这个原因引入了 flag 变量)。

为了满足此要求,我稍微更改了您的代码:

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

public class GuessingGame {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        Random generator = new Random();
        int difficulty = 0;
        int guesses = 0;
        int userGuess = 0;
        int correctAnswer = 0;
        int counter = 0;
        System.out.println("We are going to play a number guessing game.");
        System.out.println(" ");
        System.out.println("Choose your difficulty:");
        System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
        difficulty = keyboard.nextInt();
        if (difficulty == 10) {
            guesses = 3;
            System.out.println("You have 3 guesses, make them count!");
        } else if (difficulty == 25) {
            guesses = 5;
            System.out.println("You have 5 guesses, make them count!");
        } else if (difficulty == 50) {
            guesses = 6;
            System.out.println("You have 6 guesses, make them count!");
        } else {
            System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
            difficulty = (difficulty * 100);
            guesses = 1;
        }
        System.out.println(" ");
        System.out.println("Ok, I have my number. Time to play.");
        correctAnswer = generator.nextInt(difficulty) + 1;
        System.out.println("Pick a whole number between 1 and " + difficulty + ":");
        userGuess = keyboard.nextInt();
        while (counter <= guesses) {
            // int userGuessDifference = (Math.abs(correctAnswer) - Math
            //      .abs(userGuess));
                    int userGuessDifference = Math.abs(correctAnswer - userGuess);
            if (userGuess == correctAnswer) {
                System.out.println("CONGRATS YOU WIN!");
                break;
            }
            else if ((userGuessDifference <= (difficulty * .10))) {
                System.out.println("HOT!");
            }
            else if ((userGuessDifference < (difficulty * .25))
                    && (userGuessDifference > (difficulty * .10))) {
                System.out.println("Warm...");
            }
            else {
                System.out.println("Ice cold.");
            }
            userGuess = keyboard.nextInt();
            counter++;
        }
    }
}
于 2013-10-03T10:15:02.480 回答