2016-02-27 88 views
0

当用户单击X按钮或CANCEL按钮时,需要结束循环的帮助,然后单击消息对话框的OK或X。使用“取消”按钮或X按钮结束while循环

这是我的代码:

import javax.swing.*; 
public class SimpleCalculator1 
{ 

    public static void main(String [] args) 
    { 
     int error1, error2; 
     JTextField field1 = new JTextField(); 
      JTextField field2 = new JTextField(); 
      JTextField field3 = new JTextField(); 
     Object[] message = 
      { 
      "Enter first integer:", field1, 
      "Input second integer:", field2, 
      "Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3, 
      }; 
     int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION); 

     while(true) 
     { 
      if(option == JOptionPane.OK_OPTION) 
      { 
       String value1 = field1.getText(); 
       String value2 = field2.getText();   
       String operation = field3.getText(); 

       try 
       { 
        int num1 = Integer.parseInt(value1); 
        int num2 = Integer.parseInt(value2); 
       } 
       catch(NumberFormatException ne) 
       { 
        JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries 
       } 

       switch(operation = field3.getText()) 
       { 
        case "+": 
        { 
         int num1 = Integer.parseInt(value1); 
         int num2 = Integer.parseInt(value2); 
         int result = num1+num2; 
         JOptionPane.showMessageDialog(null,"The sum is: "+result); 
         break; 
        } 
       case "-": 
        { 
         int num1 = Integer.parseInt(value1); 
         int num2 = Integer.parseInt(value2); 
         int result = num1-num2; 
         JOptionPane.showMessageDialog(null,"The difference is: "+result); 
         break; 
        } 
       case "*": 
        { 
         int num1 = Integer.parseInt(value1); 
         int num2 = Integer.parseInt(value2); 
         int result = num1*num2; 
         JOptionPane.showMessageDialog(null,"The product is: "+result); 
         break; 
        } 
       case "/": 
        { 
         int num1 = Integer.parseInt(value1); 
         int num2 = Integer.parseInt(value2); 
         double result = (double)num1/num2; 
         JOptionPane.showMessageDialog(null,"The quotient is: "+result); 
         break; 
        } 

       default: 
        error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning. 
       } 
       option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION); 

      } 
      else if (option == JOptionPane.CANCEL_OPTION) 
       JOptionPane.showMessageDialog(null,"Thank you for using our program!"); 

      else 
       JOptionPane.showMessageDialog(null,"Thank you for using our program!");  
     } 
    } 
} 

回答

0

使用Break如果用户点击关闭,退出循环或取消

while(true) 
{ 
    if(option == JOptionPane.OK_OPTION) 
    { 
     String value1 = field1.getText(); 


     String value2 = field2.getText(); 


     String operation = field3.getText(); 

     try 
     { 
      int num1 = Integer.parseInt(value1); 
      int num2 = Integer.parseInt(value2); 
     } 
     catch(NumberFormatException ne) 
     { 
      JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries 
     } 
    } 
    else if(option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION){ 
     break; 
    } 
} 
+0

谢谢您的两个答案是正确的。 –

+0

@JoshuaEnriquez,我建议[“接受”](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)其中一个正确答案。 :) – Jonah

+0

我接受了Clyme的答案,我只是表示感谢回答我的问题:) –

0

我不知道Java,但简单的想法是,你需要放置一个在全局位置用真值初始化变量,并在每次迭代中检查变量是否仍然为真,如果变量为假,则停止/中断/返回循环。 ,你可以通过按钮使用onclick事件来设置该变量的值,如onclick =“variable = false;”

0

只需要创建一个布尔你进入循环之前:

boolean shouldExit = false; 
while (!shouldExit) ... 

,如果用户希望取消该程序可以设置布尔为true:

if (option == JOptionPane.CANCEL_OPTION) { 
shouldExit = true; 
} 
+0

非常感谢。现在我知道如何取消一个无限循环,当用户点击JOptionPane –

+0

上的某个按钮没问题,我很高兴它帮助你:D – Clyme