0

因此,在尝试创建一个随机数生成器以提示用户不断猜测生成的数字直到他们猜对时,我遇到了一些问题。

一切正常,直到我尝试添加再次播放的选项。问题在于,如果他们猜错了数字,它会不断重复相同的错误消息,无论他们猜的太高、太低,还是输入无效,都会不断地重复。如果第一次猜对了号码,则一切正常,并提示再次播放并完美运行。

问题是,我使用了 2 个 for 循环,因为当他们开始猜测时,我不想要:

“这个程序会生成一个从 0 到 100 的随机数,你必须猜。”

每次他们猜错答案时都输出,我只希望在他们选择再次播放选项时输出,这就是我制作嵌套 for 循环的原因。但是问题仍然存在,在显示相同答案的第一个错误猜测之后,内部 for 循环进入无限循环。

import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.UIManager;
import java.awt.*;

public class RandomNumberGuesser{
    public static void main(String[] args){
        UIManager m1=new UIManager();
        Color g = Color.gray;
        Color lg = g.brighter();
        m1.put("OptionPane.background", lg);
        m1.put("Panel.background", lg);

        int x;
        for(x = 1; true; x++){
        Random random = new Random();
        int randomNumber = random.nextInt(100);
        System.out.println(randomNumber);
        JOptionPane.showMessageDialog(null,
            "This program will generate a random number from 0 to 100 which you have to guess.",
            "Number Guesser",
            JOptionPane.INFORMATION_MESSAGE);
            String guess = JOptionPane.showInputDialog(null,
                "Guess a number.",
                "Guess",
                JOptionPane.QUESTION_MESSAGE);
                if(guess == null){
                    System.out.println("The user has terminated the program");
                    System.exit(0);
                    }
            int guess1 = Integer.parseInt(guess);

            int y;
            for(y = 1; true; y++){
                if(guess1 > 100 || guess1 < 0)
                JOptionPane.showMessageDialog(null,
                    "Guess is out of range!\nPlease enter valid input.",
                    "Invalid Input",
                    JOptionPane.WARNING_MESSAGE);

            else if(randomNumber > guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too low.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else if(randomNumber < guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too high.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else{
                JOptionPane.showMessageDialog(null,
                    "You guessed the number right!\nIt took you "+y+" attempt(s) to guess it.",
                    "Congratulations!",
                    JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.showConfirmDialog(null, "Want to play again?", "Play again?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    System.out.println("Play again soon!");
                    System.exit(0);
                }
                else{
                    y = 0;
                    break;
                    }
                }
            }
        }
    }
}
4

3 回答 3

0

使用 while 循环。

这是一个如何保持循环直到满足某些条件的示例(在这种情况下,如果一个数字介于 1 和 100 之间):

Scanner keyboard = new Scanner(System.in);
    int number;
    boolean good = false;
    do
    {
      System.out.println("Enter a number between 1 and 100: ");
      number = keyboard.nextInt();
      if(number > 0 && number <= 100) {
        System.out.println("Good choice: \"" + number + "\"!");
        good = true;
      }
      else
        System.out.println("Invalid number: \"" + number + "\"! Please enter a number between 1 and 100!\n");
    }
    while (!good);
 } 

样本输入:

-1 -2 0 1

输出:

Enter a number between 1 and 100: 
Invalid number: "-1"! Please enter a number between 1 and 100!

Enter a number between 1 and 100: 
Invalid number: "-2"! Please enter a number between 1 and 100!

Enter a number between 1 and 100: 
Invalid number: "0"! Please enter a number between 1 and 100!

Enter a number between 1 and 100: 
Good choice: "1"!
于 2013-10-27T22:38:51.110 回答
0

你可以试试这个:

public class RandomNumberGuesser {
public static void main(String[] args) {
    // UIManager m1=new UIManager();
    Color g = Color.gray;
    Color lg = g.brighter();
    UIManager.put("OptionPane.background", lg);
    UIManager.put("Panel.background", lg);

    Random random = new Random();
    int attempts = 0; // Number of attempts

    int randomNumber = random.nextInt(100);
    System.out.println(randomNumber);

    // This outside the loop so is showed just ONE time
    JOptionPane.showMessageDialog(null,
                    "This program will generate a random number from 0 to 100 which you have to guess.", "Number Guesser", JOptionPane.INFORMATION_MESSAGE);

    while (true) {

        attempts++;

        String guess = JOptionPane.showInputDialog(null, "Guess a number.",
                "Guess", JOptionPane.QUESTION_MESSAGE);
        if (guess == null) {
            System.out.println("The user has terminated the program");
            System.exit(0);
        }
        int guess1 = Integer.parseInt(guess);

            if (guess1 > 100 || guess1 < 0)
                JOptionPane
                        .showMessageDialog(
                                null,
                                "Guess is out of range!\nPlease enter valid input.",
                                "Invalid Input",
                                JOptionPane.WARNING_MESSAGE);

            else if (randomNumber > guess1)
                JOptionPane.showMessageDialog(null,
                        "You guessed too low.\nGuess again!", "Your guess",
                        JOptionPane.INFORMATION_MESSAGE);

            else if (randomNumber < guess1)
                JOptionPane.showMessageDialog(null,
                        "You guessed too high.\nGuess again!",
                        "Your guess", JOptionPane.INFORMATION_MESSAGE);

            else {
                JOptionPane
                        .showMessageDialog(null,
                                "You guessed the number right!\nIt took you "
                                        + attempts + " attempt(s) to guess it.",
                                "Congratulations!",
                                JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.showConfirmDialog(null,
                        "Want to play again?", "Play again?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    System.out.println("Play again soon!");
                    System.exit(0);
                } else {
                    randomNumber = random.nextInt(100);
                    System.out.println(randomNumber);
                    attempts = 0;
                }
            }
        }
    }
}
于 2013-10-27T22:47:47.003 回答
0

当没有猜到正确的数字时,它会进入无限循环。我对您的代码进行了一些更改,看看是否有帮助。

    package com.ananth.stackoverflow.help;

import java.awt.Color;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class RandomNumberGuesser {
    public static void main(String[] args) {
        UIManager m1 = new UIManager();
        Color g = Color.gray;
        Color lg = g.brighter();
        m1.put("OptionPane.background", lg);
        m1.put("Panel.background", lg);

        int x;
        for (x = 1; true; x++) {
            Random random = new Random();
            int randomNumber = random.nextInt(100);
            System.out.println(randomNumber);
            JOptionPane.showMessageDialog(null,
                    "This program will generate a random number from 0 to 100 which you have to guess.", "Number Guesser",
                    JOptionPane.INFORMATION_MESSAGE);
            String guess = getInputFromUser();
            int guess1 = Integer.parseInt(guess);

            int y;
            for (y = 1; true; y++) {
                if (guess1 > 100 || guess1 < 0) {
                    JOptionPane.showMessageDialog(null, "Guess is out of range!\nPlease enter valid input.",
                            "Invalid Input", JOptionPane.WARNING_MESSAGE);
                } else if (randomNumber == guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed the number right!\nIt took you " + y
                            + " attempt(s) to guess it.", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);
                    if (JOptionPane.showConfirmDialog(null, "Want to play again?", "Play again?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                        System.out.println("Play again soon!");
                        System.exit(0);
                    } else {
                        y = 0;
                        break;
                    }
                } else if (randomNumber > guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed too low.\nGuess again!", "Your guess",
                            JOptionPane.INFORMATION_MESSAGE);
                    guess = getInputFromUser();
                    guess1 = Integer.parseInt(guess);
                } else if (randomNumber < guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed too high.\nGuess again!", "Your guess",
                            JOptionPane.INFORMATION_MESSAGE);
                    guess = getInputFromUser();
                    guess1 = Integer.parseInt(guess);
                }
            }
        }
    }

    private static String getInputFromUser() {
        String guess = "";
        guess = JOptionPane.showInputDialog(null, "Guess a number.", "Guess", JOptionPane.QUESTION_MESSAGE);
        if (guess == null) {
            System.out.println("The user has terminated the program");
            System.exit(0);
        }
        return guess;
    }
}
于 2013-10-27T22:50:01.370 回答