先导包
import javax.swing.*;
声明一个main方法
public class CalDemo02 {
public static void main(String[] args) {
}
]
boolean bool = true;
while (bool) {
JOptionPane g = new JOptionPane();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Object[] options = {"加法", "减法", "乘法", "除法", "退出"};
int choice = JOptionPane.showOptionDialog(null, "请输入选项", "计算器", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
int a;
String a2;
int b;
String b2;
int c;
String c2;
switch (choice) {
case 0:
a2 = (String) JOptionPane.showInputDialog(null, "请输入第一个加数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入第二个加数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a + b;
JOptionPane.showMessageDialog(null, "和为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 1:
a2 = (String) JOptionPane.showInputDialog(null, "请输入被减数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入减数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a - b;
JOptionPane.showMessageDialog(null, "差为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 2:
a2 = (String) JOptionPane.showInputDialog(null, "请输入第一个乘数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入第二个乘数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a * b;
JOptionPane.showMessageDialog(null, "积为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 3:
a2 = (String) JOptionPane.showInputDialog(null, "请输入被除数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入除数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a / b;
JOptionPane.showMessageDialog(null, "商为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
default:
bool = false;
}
}
package com.demo06;
import javax.swing.*;
public class CalDemo02 {
public static void main(String[] args) {
boolean bool = true;
while (bool) {
JOptionPane g = new JOptionPane();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Object[] options = {"加法", "减法", "乘法", "除法", "退出"};
int choice = JOptionPane.showOptionDialog(null, "请输入选项", "计算器", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
int a;
String a2;
int b;
String b2;
int c;
String c2;
switch (choice) {
case 0:
a2 = (String) JOptionPane.showInputDialog(null, "请输入第一个加数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入第二个加数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a + b;
JOptionPane.showMessageDialog(null, "和为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 1:
a2 = (String) JOptionPane.showInputDialog(null, "请输入被减数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入减数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a - b;
JOptionPane.showMessageDialog(null, "差为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 2:
a2 = (String) JOptionPane.showInputDialog(null, "请输入第一个乘数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入第二个乘数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a * b;
JOptionPane.showMessageDialog(null, "积为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
case 3:
a2 = (String) JOptionPane.showInputDialog(null, "请输入被除数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
a = Integer.parseInt(a2);
b2 = (String) JOptionPane.showInputDialog(null, "请输入除数:", "计算器", JOptionPane.PLAIN_MESSAGE, null, null, "");
b = Integer.parseInt(b2);
c = a / b;
JOptionPane.showMessageDialog(null, "商为:" + c, "计算器", JOptionPane.PLAIN_MESSAGE);
break;
default:
bool = false;
}
}
}
}