gpt4 book ai didi

java - 从头开始重播应用程序,如何?

转载 作者:行者123 更新时间:2023-12-02 04:59:22 24 4
gpt4 key购买 nike

我对 Java 和这一切都很陌生。但我得到了这段代码,我想做的是,如果我得到一个“Yes_No_Option”,然后如果用户按“yes”,它将从头开始重新启动代码”,如果用户按“no”,它将只是关闭。感谢所有答案!

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

}//对于整体

最佳答案

您可以用 while 循环包围您的主代码,检查您初始化为 true 的 boolean 值。当用户说“否”时,将该值切换为 false,程序将退出 while 循环,或者当用户说"is"时,不执行任何操作,程序流程应在 while 循环开始时重新启动。

关于java - 从头开始重播应用程序,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com