用java编写的简易计算器:界面为:请输入第一个数,请输入第二个数,选着运算符号,加减乘除四个就行,等号
4个回答
展开全部
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;
}
}
}
}
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;
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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("输入的数不是数字");
}
}
}
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("输入的数不是数字");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-20
展开全部
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);
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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上
不错
不错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询