2014-02-08 43 views
0

我想讓我的整個程序再次循環,如果用戶按是的,但我堅持我應該把什麼。我需要它循環無限,直到用戶說不。任何幫助將不勝感激。Java 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 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

做出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); 
    } 
} 
} 
+0

它告訴我,我我這樣做時有重複的變量。有什麼辦法可以重置我的變量「輸入」和「輸出」的值? – Javier

+0

刪除最後兩個,而是使用字符串檢查來代替,這意味着如果是,請求繼續或不是'flag = true'否則'flag = false' –

+0

明白了!謝謝! – Javier