如何在java中重置for循环的计数器

时间:2013-10-27 20:30:48

标签: java for-loop counter reset joptionpane

所以我想在高中为我的班级创建一个程序,以便能够生成一个随机数,并让用户继续猜测,直到他们做对了。

到目前为止,我已经完成了一切,除非我提示他们再次进行游戏,计算他们猜对了多少次的计数器在他们选择再次玩之后不会重置。我不知道该怎么做,并且在寻找解决方案时,对我来说理解或使用它太复杂了。

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

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

            }

        }
    }
}

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

counter=0;