从一开始就重播应用程序,如何?

时间:2015-02-10 19:21:56

标签: java joptionpane

我是Java的新手。但我得到了这个代码,我想要做的是如果我得到“Yes_No_Option”然后如果用户按下“是”它将从开始“重新启动代码”,如果用户按下“否​​”它将只是关。 感谢所有答案!

public class JOptionMain {
     static Scanner sf = new Scanner(System.in);
     static String low,high, guessStr;
     static int x, y, guess, rnd;

public static void main(String[] args) {
    low = JOptionPane.showInputDialog("Enter the lowest number: ");
    high = JOptionPane.showInputDialog("Enter the highest number: ");
    try{
        x = Integer.parseInt(low);
        y = Integer.parseInt(high);
        guessStr = JOptionPane.showInputDialog("Guess the number between " + x + " and " + y);
        guess = Integer.parseInt(guessStr);
    }catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null, "You entered something wrong, app will now exit!" , "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }
    rnd = randomGen(y, x);
    boolean bool = check(guess, rnd);
    if (bool == true){
        JOptionPane.showMessageDialog(null, "Congrats! You entered the correct number", "Correct", JOptionPane.INFORMATION_MESSAGE);
    }else{
        JOptionPane.showMessageDialog(null, "Im Sorry, you entered the wrong number. The correct number was " + rnd);
    }
} //For Main

public static int randomGen(int high, int low){
    int random = (int)(Math.random()*(high - low + 1) + low);
    return random;
}

public static boolean check(int num1, int num2){
    if(num1 == num2){
        return true;
    }else{
        return false;
    }
}

} //对于整个

1 个答案:

答案 0 :(得分:2)

您可以使用while循环检查一个布尔值,并将其初始化为true,从而包围您的主代码。当用户说“不”时,将该值切换为false,程序将退出while循环,或者当他们说“是”时,不执行任何操作,程序流程应在while循环开始时重新启动。