Java JOptionPane整个程序循环
我试图让我的整个程序再次循环,如果用户按“是”,但我坚持我应该放什么。我需要它无限循环,直到用户说不。任何帮助将不胜感激Java JOptionPane整个程序循环,java,loops,while-loop,joptionpane,Java,Loops,While Loop,Joptionpane,我试图让我的整个程序再次循环,如果用户按“是”,但我坚持我应该放什么。我需要它无限循环,直到用户说不。任何帮助将不胜感激 public static void main(String[] args) { // TODO Auto-generated method stub String xString = JOptionPane.showInputDialog(null, "Please input the numbers you want to be
public static void main(String[] args) {
// TODO Auto-generated method stub
String xString =
JOptionPane.showInputDialog(null, "Please input the numbers you want to be converted into Roman numerals. "
+ "Must be between 1000 and 3000");
int input = Integer.parseInt(xString);
String output = "";
String zero = "Sorry but the number must be between 1000 and 3000.";
while (input == 0){
JOptionPane.showMessageDialog(null, zero);
System.exit(0);
}
while (input > 999 && input < 3001 && input != 0)
{
while (input >= 1000){
output += "M";
input -= 1000;}
while (input >= 900){
output += "CM";
input -= 900;}
while (input >= 500){
output += "D";
input -= 500;}
while (input >= 400){
output += "CD";
input -= 400;}
while (input >= 100){
output += "C";
input -= 100;}
while (input >= 90){
output += "XC";
input -= 90;}
while (input >= 50){
output += "L";
input -= 50;}
while (input >= 40){
output += "XL";
input -= 40;}
while (input >= 10){
output += "X";
input -= 10;}
while (input >= 9){
output += "IX";
input -= 9;}
while (input >= 5){
output += "V";
input -= 5;}
while (input >= 4){
output += "IV";
input -= 4;}
while (input >= 1){
output += "I";
input -= 1;}
}
String message, incorrect, tryagain;
message = "The numerals are " + output +".";
incorrect = "Sorry but the number must be between 1000 and 3000.";
if (input == 0)
JOptionPane.showMessageDialog(null, message);
else
JOptionPane.showMessageDialog(null, incorrect);
tryagain = "Would you like to try again?";
int again = JOptionPane.showConfirmDialog(null, tryagain, null, JOptionPane.YES_NO_OPTION);
while (again == JOptionPane.YES_OPTION){
JOptionPane.showInputDialog(null, /*need help here*/);
}
while (again == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Closing...");
System.exit(0);
}
}
创建如下布尔参数:
Bollean flag =false;
while (again == JOptionPane.YES_OPTION){
JOptionPane.showInputDialog(null, /*need help here*/);
flag=true;
}
while (again == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Closing...");
flag=false;
System.exit(0);
}
并按如下方式更改代码:
Bollean flag =false;
while (again == JOptionPane.YES_OPTION){
JOptionPane.showInputDialog(null, /*need help here*/);
flag=true;
}
while (again == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Closing...");
flag=false;
System.exit(0);
}
因此,在这之后,请执行以下操作:
while(flag){
String xString =
/*.
.
.
*/
System.exit(0);
}
}
}
当我这样做时,它告诉我有重复的变量。有什么方法可以重置变量输入和输出的值吗?请删除最后两个,而改用字符串检查,这意味着如果yes flag=true else flag=false,请询问是否继续