计算器加法java代码 计算器加法java代码

编写java程序简单计算器主要涉及计算器加法java代码的知识点: 类计算器加法java代码的写法, 以及方法计算器加法java代码的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.
参考代码如下
//Number类
class Number {
private int n1;//私有的整型数据成员n1
private int n2;//私有的整型数据成员n2
// 通过构造函数给n1和n2赋值
public Number(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
// 加法
public int addition() {
return n1n2;
}
// 减法
public int subtration() {
return n1 - n2;
}
// 乘法
public int multiplication() {
return n1 * n2;
}
// 除法 (可能除不尽,所以使用double作为返回类型)
public double division() {
return n1 * 1.0 / n2; // 通过n1*1.0 把计算结果转换成double类型.
}
}
//Exam4 类
public class Exam4{
public static void main(String[] args) {
Number number=new Number(15, 6);//创建Number类的对象
//下面的是调用方法得到返回值进行输出显示
System.out.println("加法" number.addition());
System.out.println("减法" number.subtration());
System.out.println("乘法" number.multiplication());
System.out.println("除法" number.division());
}
}
java加法计算器代码import java.awt.event.ActionEvent;
import java.awt.event.ActionListener
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
result = new javax.swing.JTextField();
addBtn = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Num1:");
jLabel2.setText("Num2:");
jLabel3.setText("Num3:");
addBtn.setText("Add");
addBtn.addActionListener(new jisuanAC());
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addBtn)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(result)
.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGap(168, 168, 168))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(61, 61, 61)
.addComponent(addBtn)
.addContainerGap(81, Short.MAX_VALUE))
);
jMenu1.setText("Operation");
jMenu3.setText("Add");
jMenu1.add(jMenu3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Exit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// /editor-fold
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private class jisuanAC implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getSource()== addBtn)
{
int number1 = Integer.parseInt(num1.getText());
int number2 = Integer.parseInt(num2.getText());
int rs = number1number2;
result.setText(String.valueOf(rs));
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField result;
}
这是只有一个加法的例子!希望帮到你
求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算 。(public
MyCalculator()
{
f
=
new
JFrame("计算器ByMdou");
Container
contentPane
=
f.getContentPane();
/****************菜单的创建开始**************************************/
JMenuBar
mBar
=
new
JMenuBar();
mBar.setOpaque(true);
mEdit
=
new
JMenu("编辑(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy
=
new
JMenuItem("复制(C)");
mEdit.add(mCopy);
mPaste
=
new
JMenuItem("粘贴(P)");
mEdit.add(mPaste);
mView
=
new
JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new
JMenuItem("标准型"));
mView.add(new
JMenuItem("科学型"));
mView.addSeparator();
mView.add(new
JMenuItem("查看分组"));
mHelp
=
new
JMenu("帮助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new
JMenuItem("帮助主题"));
mHelp.addSeparator();
mHelp.add(new
JMenuItem("关于计算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new
BorderLayout
());
JPanel
pTop
=
new
JPanel();
tResult
=
new
JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT
);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel
pBottom
=
new
JPanel();
pBottom.setLayout(new
BorderLayout());
JPanel
pLeft
=
new
JPanel();
pLeft.setLayout(new
GridLayout(5,1,3,3));
bM
=
new
JButton("
");
bM.setEnabled(false);
pLeft.add(bM);
bOther
=
new
JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("M ");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
用java写个简单的加减计算器不仅可以加减还可以乘除,代码如下:
package 计算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
boolean flag = false;
JTextField jtf = new JTextField();
public static void main(String[] args) {
new Calculator();
}
public Calculator() {
setSize(300, 300);
setTitle("计算器");
Container c = getContentPane();
jtf.setHorizontalAlignment(JTextField.RIGHT);
JButton jb = new JButton("=");
jb.addActionListener(this);
JPanel jp = new JPanel();
c.add(jtf, BorderLayout.NORTH);
c.add(jp, BorderLayout.CENTER);
c.add(jb, BorderLayout.SOUTH);
jp.setLayout(new GridLayout(4, 4));
addButton(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addButton(JPanel jp) {
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp.add(b);
b = new JButton("2");
b.addActionListener(this);
jp.add(b);
b = new JButton("3");
b.addActionListener(this);
jp.add(b);
b = new JButton(" ");
b.addActionListener(this);
jp.add(b);
b = new JButton("4");
b.addActionListener(this);
jp.add(b);
b = new JButton("5");
b.addActionListener(this);
jp.add(b);
b = new JButton("6");
b.addActionListener(this);
jp.add(b);
b = new JButton("-");
b.addActionListener(this);
jp.add(b);
b = new JButton("7");
b.addActionListener(this);
jp.add(b);
b = new JButton("8");
b.addActionListener(this);
jp.add(b);
b = new JButton("9");
b.addActionListener(this);
jp.add(b);
b = new JButton("*");
b.addActionListener(this);
jp.add(b);
b = new JButton("0");
b.addActionListener(this);
jp.add(b);
b = new JButton(".");
b.addActionListener(this);
jp.add(b);
b = new JButton("C");
b.addActionListener(this);
jp.add(b);
b = new JButton("/");
b.addActionListener(this);
jp.add(b);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "=") {
if (jtf.getText().matches("\\d \\.?\\d*[\\ \\-\\*\\/]\\d \\.?\\d*")) {
String s = jtf.getText();
Pattern p = Pattern.compile("\\d \\.?\\d*");
Matcher m = p.matcher(s);
m.find();
double firstNum = Double.parseDouble(m.group());
m.find();
double secondNum = Double.parseDouble(m.group());
if (jtf.getText().matches("\\d \\.?\\d*\\ \\d \\.?\\d*")) {
jtf.setText(""(firstNumsecondNum));
} else if (jtf.getText().matches("\\d \\.?\\d*\\-\\d \\.?\\d*")) {
jtf.setText(""(firstNum - secondNum));
} else if (jtf.getText().matches("\\d \\.?\\d*\\*\\d \\.?\\d*")) {
jtf.setText(""(firstNum * secondNum));
} else if (jtf.getText().matches("\\d \\.?\\d*\\/\\d \\.?\\d*")) {
jtf.setText(""(firstNum / secondNum));
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确表达式!");
}
flag = true;
} else if (e.getActionCommand() == "C") {
jtf.setText("");
} else {
if (flag) {
if (e.getActionCommand() != " "e.getActionCommand() != "-"
e.getActionCommand() != "*"
e.getActionCommand() != "/") {
//System.out.println(e.getActionCommand() != " ");
jtf.setText("");
}
flag = false;
}
jtf.setText(jtf.getText()e.getActionCommand());
}
}
}
谁能给一个Java的计算器程序代码(加注释 )以前写的,给你了
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE");
//删除
private JButton C = new JButton("C");
//四则运算符号键
private JButton add = new JButton(" ");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args) {
new Calculator();//new计算器实例
}
public Calculator(){
//添加组件 , 布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注册各个组件监听事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注册四则运算监听
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注册数字监听事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否则的话,把0添加到后面,譬如文本框是1 , 结果就为10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格键,删除一个直到没有字符删除
String inputText = input.getText();
if(inputText.length()0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有运算数字和符号
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE--将文本框置为0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小数点 , 如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等号
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals(" ")){//执行加法运算
result = Double.parseDouble(num1)Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//减法运算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法运算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法运算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
//格式化最终结果,保留两位小数点
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
帮忙用java补齐制作计算器加减乘除等于小数点这几个运算符的代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
/**
* java swing计算器
* @author young
*
*/
public class Calculator extends Frame implements ActionListener // 计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint, positiveminus, backspace, reciprocal, equal, clear; // 声明成员变量
// 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[] = { " ", "-", "*", "/" };
LinkedList linklist;
boolean pressequal = false;
public Calculator() // 构造方法
{
super("计算器");
linklist = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i = 9; i) {
numberButton[i] = new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton = new OperatorButton[4];
for (int i = 0; i4; i) {
operatorButton[i] = new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint = new Button(".");
positiveminus = new Button(" /-");
backspace = new Button("CE");
reciprocal = new Button("1/x");
equal = new Button("=");
clear = new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result = new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman", Font.PLAIN, 14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel = new Panel();
panel.setLayout(new GridLayout(4, 5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result, "North");
add(panel, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(270, 200);
setLocation(300, 230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) // 按钮的单击事件处理方法
{
if (e.getSource() instanceof NumberButton) // 数字按钮
{
NumberButton b = (NumberButton) e.getSource();
if (linklist.size() == 0) {
int number = b.getNumber();
linklist.add(""number);
result.setText(""number);
pressequal = false;
}
else if (linklist.size() == 1pressequal == false) {
int number = b.getNumber();
String num = (String) linklist.getFirst();
String s = num.concat(""number);
linklist.set(0, s);
result.setText(s);
} else if (linklist.size() == 1pressequal == true) {
int number = b.getNumber();
linklist.removeFirst();
linklist.add(""number);
pressequal = false;
result.setText(""number);
} else if (linklist.size() == 2) {
int number = b.getNumber();
linklist.add(""number);
result.setText(""number);
} else if (linklist.size() == 3) {
int number = b.getNumber();
String num = (String) linklist.getLast();
String s = num.concat(""number);
linklist.set(2, s);
result.setText(s);
}
} else if (e.getSource() instanceof OperatorButton) // 操作按钮
{
OperatorButton b = (OperatorButton) e.getSource();
if (linklist.size() == 1) {
String fuhao = b.getOperator();
linklist.add(fuhao);
} else if (linklist.size() == 2) {
String fuhao = b.getOperator();
linklist.set(1, fuhao);
} else if (linklist.size() == 3) {
String fuhao = b.getOperator();
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals(" ")) {
n = n1n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
linklist.clear();
linklist.add(""n);
linklist.add(fuhao);
result.setText(""n);
} catch (Exception ee) {
}
}
} else if (e.getSource() == equal) // 等号按钮
{
pressequal = true;
if (linklist.size() == 1 || linklist.size() == 2) {
String num = (String) linklist.getFirst();
result.setText(""num);
} else if (linklist.size() == 3) {
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals(" ")) {
n = n1n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
result.setText(""n);
linklist.set(0, ""n);
linklist.removeLast();
linklist.removeLast();
} catch (Exception ee) {
}
}
} else if (e.getSource() == radixpoint) // 小数点按钮
{
if (linklist.size() == 0) {
pressequal = false;
} else if (linklist.size() == 1) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getFirst();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(0, s);
} else {
s = num;
}
linklist.set(0, s);
result.setText(s);
}
else if (linklist.size() == 3) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getLast();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(2, s);
} else {
s = num;
}
result.setText(s);
}
} else if (e.getSource() == backspace) // 退格按钮
{
if (linklist.size() == 1) {
String num = (String) linklist.getFirst();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(0, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
} else if (linklist.size() == 3) {
String num = (String) linklist.getLast();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(2, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
}
} else if (e.getSource() == positiveminus) // 正负号按钮
{
if (linklist.size() == 1) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(2, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == reciprocal) // 求倒数按钮
{
if (linklist.size() == 1 || linklist.size() == 2) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == clear) // 清零按钮
{
pressequal = false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[]) {
new Calculator();
}
}
class NumberButton extends Button // 数字按钮类
{
int number;
public NumberButton(int number) // 构造方法
{
super(""number);
this.number = number;
setForeground(Color.blue);
}
public int getNumber() {
return number;
}
}
class OperatorButton extends Button // 运算符号按钮类
{
String operator;
public OperatorButton(String operator) // 构造方法
{
super(operator);
this.operator = operator;
setForeground(Color.red);
}
public String getOperator() {
return operator;
}
}
【计算器加法java代码 计算器加法java代码】关于计算器加法java代码和计算器加法java代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读