문제

I am trying to get my whole program to loop again if the user presses yes but I am stuck on what I should be putting. I need it to loop infinitely until the user says no. Any help will be appreciated.

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);
    }
}
도움이 되었습니까?

해결책

make a Boolean parameter like this:

Bollean flag =false;

and change your code like this:

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

so after that do this:

while(flag){
  String xString = 
  /*.
    .
    .
   */
 System.exit(0);
  }
 }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top