我试图让我的整个程序再次循环,如果用户按下是,但我坚持我应该放的东西。我需要它无限循环,直到用户说不。任何帮助将不胜感激。
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);
}
}
答案 0 :(得分:0)
像这样制作Boolean parameter
:
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);
}
}
}