如何从出错后的位置启动Java程序

问题描述 投票:0回答:2

在下面的程序中,我创建了一个简单的数字游戏,选择 1 到 100 之间的数字并要求用户猜测该数字。用户可以继续猜测,直到尝试了 10 次为止。

当我运行程序并输入字符串时,程序抛出 NumberFormatException。单击“确定”后,我找不到处理此问题的方法,因此我调用了“TryAgain”方法。我想知道的是,在我不小心按下带有字符串文本的按钮后,如何以相同的尝试次数重新启动程序。

import javax.swing.JOptionPane;

public class NumberGame {
    static int scoreOfWins = 0; 
    static int numberOfAttempt = 0;
    public static void main(String[] args) {
        Game();
    }

    static void Game() {
        try {
            int num = (int) (Math.random() * 100);
            int count = 10;
            while(count > 0) {
//              System.out.println(num);
                String inputTitle = "Guess the number between 1 to 100";
                String input = JOptionPane.showInputDialog(inputTitle, "Enter your answer here...");
                if (input == null) {
                    System.exit(0);
                }
                int intInput = Integer.parseInt(input);
                System.out.println(intInput);
                if (intInput == num) {
                    numberOfAttempt = 11 - count;
                    JOptionPane.showMessageDialog(null, "You win! Your guess was right after " + numberOfAttempt + " attempts");
                    scoreOfWins++;
                    TryAgain();
                    return;
                } else if (intInput > num) {
                    String numberOfTimes = " You can try " + (count - 1) + " times";
                    JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a lower value." + numberOfTimes);
                    count--;
                } else if (intInput < num){
                    String numberOfTimes = " You can try " + (count - 1) + " times";
                    JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a higher value." + numberOfTimes);
                    count--;
                }
            }
            JOptionPane.showMessageDialog(null, "You Lost the game!");
            TryAgain();
        } catch (Exception E) {
            JOptionPane.showMessageDialog(null, "Number format exception detected, Please only input numbers!");
            TryAgain();
        }
    }
    static void TryAgain() {
        while (true) {
            String inputAns = JOptionPane.showInputDialog("Type yes if you want to containue!"
                    + " Type No if you want to give up!", "you won " + scoreOfWins + " times do you want to continue?");
            if (inputAns == null) {
                System.exit(0);
            }
            inputAns = inputAns.toUpperCase();
            if (inputAns.equals("Y") | inputAns.equals("Yes")) {
                Game();
            } else if (inputAns.equals("N") |inputAns.equals("NO")) {
                JOptionPane.showMessageDialog(null, "OKAY you won " + scoreOfWins + " times today, see you later!");
                return;
            } else {
                JOptionPane.showMessageDialog(null, "Please provide a good valid identifier like Yes or NO");
            }
        }
    }
}
java joptionpane
2个回答
2
投票

这是您的代码,正在执行您想要的操作(可能不是最有效的方法)

public class NumberGame {

    static int scoreOfWins = 0; 
    static int numberOfAttempt = 0;
    
    public static void main(String[] args) {
        game();
    }
    
    static int num = new Random().nextInt(100) + 1;
    static int count = 10;

    static void game() {
        try {
            while(count > 0) {
                String inputTitle = "Guess the number between 1 to 100";
                String input = JOptionPane.showInputDialog(inputTitle, "Enter your answer here...");
                if (input == null) {
                    System.exit(0);
                }
                int intInput = Integer.parseInt(input);
                System.out.println(intInput);
                if (intInput == num) {
                    numberOfAttempt = 11 - count;
                    JOptionPane.showMessageDialog(null, "You win! Your guess was right after " + numberOfAttempt + " attempts");
                    scoreOfWins++;
                    tryAgain();
                    return;
                } else if (intInput > num) {
                    String numberOfTimes = " You can try " + (count - 1) + " times";
                    JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a lower value." + numberOfTimes);
                    count--;
                } else if (intInput < num){
                    String numberOfTimes = " You can try " + (count - 1) + " times";
                    JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a higher value." + numberOfTimes);
                    count--;
                }
            }
            JOptionPane.showMessageDialog(null, "You Lost the game!");
            tryAgain();
        } catch (Exception e) {
            
            if(e instanceof NumberFormatException) {
                JOptionPane.showMessageDialog(null, "Number format exception detected, Please only input numbers!");
                game();
            }
            
            // Other exception handling here
        }
    }
    
    static void restart() {
        count = 10;
        game();
    }
    
    static void tryAgain() {
        while (true) {
            String inputAns = JOptionPane.showInputDialog("Type yes if you want to containue!"
                    + " Type No if you want to give up!", "you won " + scoreOfWins + " times do you want to continue?");
            if (inputAns == null) {
                System.exit(0);
            }
            inputAns = inputAns.toUpperCase();
            if (inputAns.equalsIgnoreCase("y") | inputAns.equalsIgnoreCase("yes")) {
                restart();
            } else if (inputAns.equalsIgnoreCase("n") |inputAns.equalsIgnoreCase("no")) {
                JOptionPane.showMessageDialog(null, "OKAY you won " + scoreOfWins + " times today, see you later!");
                return;
            } else {
                JOptionPane.showMessageDialog(null, "Please provide a good valid identifier like Yes or NO");
            }
        }
    }
}

• 因此,我在您的代码中修复的第一件事是一个小但值得练习的好东西,那就是命名约定。这是非常不言自明的。

• 其次,我用

inputAns.equals("Yes")
inputAns.equals("NO")
替换了
inputAns.equalsIgnoreCase("yes")
inputAns.equalsIgnoreCase("no")
,这允许用户以他们想要的任何方式键入“yes”,甚至“yeS”。

• 接下来我实现了

new Random().nextInt(100) + 1
,这可能是生成随机数的更好方法。 这是一个很酷的小问题如果您想了解更多信息,您可能需要考虑一下。

• 我还使用

instanceof
检查了抛出的异常,这样您就可以管理更多异常,稍后您可能会发现这些异常很有帮助。

除了从游戏方法中删除

num
count
并将其设为一个字段之外,我通常没有更改您的很多代码。现在,每次运行游戏方法时,计数都不会改变,只有当您重新启动游戏时才会改变。


0
投票

“...在我不小心按下带有字符串文本的按钮后,如何以相同的尝试次数重新启动程序。...”

这是一个例子。

Random r = new Random();
Scanner in = new Scanner(System.in);
String s;
int v = r.nextInt(1, 101), g, t = 0;
boolean exit = false;
do {
    out.print("guess:  ");
    s = in.nextLine();
    try {
        if (s.isBlank()) throw new Exception();
        g = Integer.parseInt(s);
        if (g != v) {
            out.print("wrong");
            if (++t != 10) out.printf(", %s tries remaining%n", 10 - t);
            else exit = true;
        }
        else {
            out.println("correct");
            exit = true;
        }
    } catch (Exception e) {
        out.printf("invalid input '%s', try again%n", s);
    }
} while (!exit);
© www.soinside.com 2019 - 2024. All rights reserved.