当用户点击X按钮或CANCEL按钮然后单击消息对话框的OK或X时,我需要帮助来结束循环。
这是我的代码:
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 :(得分:0)
如果用户点击关闭或取消
,请使用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;
}
}
退出循环
{{1}}
答案 1 :(得分:0)
我不确定java但是简单的想法是你需要在全局放置一个用true值初始化的变量,并且在每次迭代中检查该变量是否仍然为真,如果变量为false则停止/中断/返回循环。 并且您可以使用onclick事件按钮设置该变量的值,例如onclick =“variable = false;”
答案 2 :(得分:0)
在进入循环之前创建一个布尔值:
boolean shouldExit = false;
while (!shouldExit) ...
如果用户想要取消该程序,可以将布尔值设置为true:
if (option == JOptionPane.CANCEL_OPTION) {
shouldExit = true;
}