从头开始重播应用程序,如何?
Replay the app from the start, how to?
我是 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 的布尔值。当用户说“不”时,将该值切换为 false,程序将退出 while 循环,或者当他们说“是”时,什么也不做,程序的流程应该在 while 循环开始时重新开始。
我是 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 的布尔值。当用户说“不”时,将该值切换为 false,程序将退出 while 循环,或者当他们说“是”时,什么也不做,程序的流程应该在 while 循环开始时重新开始。