用java编写的简易计算器:界面为:请输入第一个数,请输入第二个数,选...
发布网友
发布时间:2022-05-10 01:51
我来回答
共4个回答
热心网友
时间:2022-04-21 03:52
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class MainView extends JFrame implements ActionListener{
private JTextField number1 = null;
private JComboBox op = null;
private JTextField number2 = null;
private JButton eq = null;
private JLabel result = null;
public MainView(){
this.setLayout(new FlowLayout(FlowLayout.LEFT));
number1 = new JTextField(5);
op = new JComboBox(new String[]{"+","-","×","÷"});
number2 = new JTextField(5);
eq = new JButton("=");
eq.addActionListener(this);
result = new JLabel();
this.add(number1);
this.add(op);
this.add(number2);
this.add(eq);
this.add(result);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(280, 80);
this.setLocationRelativeTo(null);
this.setTitle("简易计算器");
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new MainView();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == eq){
double n1 = 0.0;
double n2 = 0.0;
try{
n1 = Double.parseDouble(number1.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "数字1不是正确的数字格式");
}
try{
n2 = Double.parseDouble(number2.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "数字2不是正确的数字格式");
}
int p = op.getSelectedIndex();
switch(p){
case 0:
result.setText(String.valueOf(n1+n2));
break;
case 1:
result.setText(String.valueOf(n1-n2));
break;
case 2:
result.setText(String.valueOf(n1*n2));
break;
case 3:
result.setText(String.valueOf(n1/n2));
break;
}
}
}
}
热心网友
时间:2022-04-21 05:10
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class EasyC {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//能实现控制台输入的语句
try {
str = in.readLine();
int num = Integer.parseInt(str);
str = in.readLine();
int num1 = Integer.parseInt(str);
System.out.println("请输入运算符号:");
str = in.readLine();
if(str.length() == 1){
char c = str.charAt(0);
switch(c){
case '+':
System.out.println(num+num1);
break;
case '-':
System.out.println(num-num1);
break;
case '*':
System.out.println(num*num1);
break;
case '/':
System.out.println(num/num1);
break;
default:
System.out.println("请输入正确的运算符");
break;
}
}
else{
System.out.println("请输入正确的运算符!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e){
System.out.println("输入的数不是数字");
}
}
}
热心网友
时间:2022-04-21 06:45
private JTextField number1 = null;
private JComboBox op = null;
private JTextField number2 = null;
private JButton eq = null;
private JLabel result = null;
public MainView(){
this.setLayout(new FlowLayout(FlowLayout.LEFT));
number1 = new JTextField(5);
op = new JComboBox(new String[]{"+","-","×","÷"});
number2 = new JTextField(5);
eq = new JButton("=");
eq.addActionListener(this);
result = new JLabel();
this.add(number1);
this.add(op);
热心网友
时间:2022-04-21 08:36
楼上
不错