gpt4 book ai didi

Java 简单计算器

转载 作者:搜寻专家 更新时间:2023-11-01 01:44:02 25 4
gpt4 key购买 nike

我用 Java 制作了这个计算器程序。仅当同时计算两个数字时,此方法才有效。这意味着要获得 1+2+3 的总和,您必须这样做:按 1按+按 2按=按+按 3按=

并计算为 6。

但我想对此进行编程,以便我可以通过以下方式获得答案:按 1按+按 2按+按 3按=

但这给出了答案 5!!!如何对其进行编码,使其像普通计算器一样工作?

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class cal1 extends JFrame {

double op1 = 0d, op2 = 0d;
double result = 0d;
char action;
boolean b = false;
boolean pressequal = false;

public cal1() {
makeUI();
}

private void makeUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);

b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bDot = new JButton(".");
bMul = new JButton("*");
bDiv = new JButton("/");
bPlus = new JButton("+");
bMinus = new JButton("-");
bEq = new JButton("=");

t = new JTextField(12);
t.setFont(new Font("Tahoma", Font.PLAIN, 24));
t.setHorizontalAlignment(JTextField.RIGHT);

numpad = new JPanel();
display = new JPanel();

numpad.add(b7);
numpad.add(b8);
numpad.add(b9);
numpad.add(bMul);
numpad.add(b4);
numpad.add(b5);
numpad.add(b6);
numpad.add(bDiv);
numpad.add(b1);
numpad.add(b2);
numpad.add(b3);
numpad.add(bMinus);
numpad.add(bDot);
numpad.add(b0);
numpad.add(bEq);
numpad.add(bPlus);

numpad.setLayout(new GridLayout(4, 5, 5, 4));

display.add(t);
add(display, BorderLayout.NORTH);
add(numpad, BorderLayout.CENTER);

t.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
typeOnt(e);
}
});

b0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b0pressed(e);
}
});

b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b1pressed(e);
}
});

b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b2pressed(e);
}
});

b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b3pressed(e);
}
});

b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b4pressed(e);
}
});

b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b5pressed(e);
}
});

b6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b6pressed(e);
}
});

b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b7pressed(e);
}
});

b8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b8pressed(e);
}
});

b9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b9pressed(e);
}
});

bDot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bDotpressed(e);
}
});

bPlus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bPlusPressed(e);
}
});

bMinus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bMinusPressed(e);
}
});

bMul.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bMulPressed(e);
}
});

bDiv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bDivPressed(e);
}
});

bEq.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bEqpressed(e);
}
});

}

void typeOnt(KeyEvent e) {
e.consume();
}

void b0pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "0");
} else {
t.setText(t.getText() + "0");
}
}

void b1pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "1");
} else {
t.setText(t.getText() + "1");
}
}

void b2pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "2");
} else {
t.setText(t.getText() + "2");
}
}

void b3pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "3");
} else {
t.setText(t.getText() + "3");
}
}

void b4pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "4");
} else {
t.setText(t.getText() + "4");
}
}

void b5pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "5");
} else {
t.setText(t.getText() + "5");
}
}

void b6pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "6");
} else {
t.setText(t.getText() + "6");
}
}

void b7pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "7");
} else {
t.setText(t.getText() + "7");
}
}

void b8pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "8");
} else {
t.setText(t.getText() + "8");
}
}

void b9pressed(ActionEvent e) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "9");
} else {
t.setText(t.getText() + "9");
}
}

void bDotpressed(ActionEvent e) {
if (!t.getText().contains(".")) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + "0.");
} else if (t.getText().isEmpty()) {
t.setText("0.");
} else {
t.setText(t.getText() + ".");
}
}
}

void bPlusPressed(ActionEvent e) {
b = true;
action = '+';
op1 = Double.parseDouble(t.getText());

}

void bMinusPressed(ActionEvent e) {
b = true;
action = '-';
op1 = Double.parseDouble(t.getText());
}

void bMulPressed(ActionEvent e) {
b = true;
action = '*';
op1 = Double.parseDouble(t.getText());
}

void bDivPressed(ActionEvent e) {
b = true;
action = '/';
op1 = Double.parseDouble(t.getText());
}

void bEqpressed(ActionEvent e) {
op2 = Double.parseDouble(t.getText());
doCal();
}

void doCal() {
switch (action) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
}
t.setText(String.valueOf(result));
}

public static void main(String[] args) {
new cal1().setVisible(true);
}

JButton b0;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
JButton b6;
JButton b7;
JButton b8;
JButton b9;
JButton bDot;
JButton bPlus;
JButton bMinus;
JButton bMul;
JButton bDiv;
JButton bEq;
JPanel display;
JPanel numpad;
JTextField t;
}

最佳答案

Java 类中的第一个应该以首字母大写开头 Java Code Conventions

其次不是为每个数字创建一个方法

void bpressed(ActionEvent e, Integer number) {
if (b) {
t.setText(null);
b = false;
t.setText(t.getText() + number);
} else {
t.setText(t.getText() + number);
}
}

为事物使用声明性名称,b 是什么?它不直观,类名应该是 Calculator 或类似的东西,代码必须是人类可读的。

与您的问题相关,当某些操作正在进行时,您可以得到一个 partialResult。例如:

你声明

private double partialResult=0D;

还有类似这样的方法

private void calculatePartialResult(Integer numberSelected){
//choose depends on action
partialResult op= numberSelected; (where op in + - * /)
}

关于Java 简单计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com