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