1

因此,我正在尝试为我在高中的班级创建一个程序,以便能够生成一个随机数并让用户不断猜测,直到他们猜对为止。

到目前为止,我的一切都很顺利,除了当我提示他们再次玩时,计算他们猜了多少次的计数器在他们选择再次玩后不会重置。我不知道该怎么做,寻找解决方案,这对我来说太复杂了,无法理解或使用。

请注意,这只是我编程的第二个月,所以代码可能看起来很业余,而我的编程可能看起来很糟糕。我只需要一个解决方案,也许还需要一个解释。

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);

        for(x = 1; true; x++){
        Random random = new Random();
        int randomNumber = random.nextInt(100);

        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);

            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 if(randomNumber == guess1){
                JOptionPane.showMessageDialog(null,
                    "You guessed the number right!\nIt took you "+x+" 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.exit(0);
                }

            }

        }
    }
}
4

2 回答 2

1
if (JOptionPane.showConfirmDialog(
                   null, 
                   "Want to play again?", 
                   "Play again?",
                   JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
    System.exit(0);
} else {
    x = 1;
}

只需将计数器重置为其初始值。

请注意,您应该避免Random每次迭代都创建一个新对象。在循环之外创建一次,random.nextInt(100)每次都调用。

于 2013-10-27T20:33:52.000 回答
0

在开始迭代之前的循环结束时,只需将您的计数器初始化为计数器开始之前的初始值。这确保在每个循环中,计数器的值都是初始位置

counter=0;
于 2017-05-02T08:52:26.080 回答