java计算器逻辑_Java计算器

99行代码

778d63787fca10f909f187d13b2fb0b2.png

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator extends JFrame {

private String[] labels = { "%","√","x²","1/x","CE","C","←","÷",

"7","8","9","×","4","5","6","-","1","2","3","+","±","0",".","=" };

private String a = "", b = "", op = "";

JPanel panel;

JTextField comp;

public Calculator() {

setLayout(new BorderLayout());

Font font = new Font("Sanserif", Font.BOLD, 25);

comp = new JTextField("0");

comp.setHorizontalAlignment(JTextField.RIGHT);

comp.setBounds(10, 8, 271, 50);

comp.setFont(font);

panel = new JPanel();

panel.setLayout(null);

panel.add(comp);

for (int i = 0; i < 24; i++) {

JButton button = new JButton(labels[i]);

if (labels[i].length() == 1 && labels[i].charAt(0) >= '0'

&& labels[i].charAt(0) <= '9' || labels[i] == ".") {

button.addActionListener(new InsertAction());

button.setFont(font);

}

else button.addActionListener(new CommandAction());

int x = i % 4 * 70 + 10, y = i / 4 * 48 + 70;

button.setLayout(null);

button.setBounds(x, y, 60, 40);

panel.add(button);

}

add(panel, BorderLayout.CENTER);

}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent e) {

String s = e.getActionCommand();

if (s != "." || b.indexOf(".") == -1) b += s;

comp.setText(b);

}

}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent e) {

String s = e.getActionCommand();

if (s == "+" || s == "-" || s == "×" || s == "÷") {

a = b; b = ""; op = s;

comp.setText(s);

}

else if (s == "=") {

double A = Double.parseDouble(a);

double B = Double.parseDouble(b);

switch (op) {

case "+":A += B; break;

case "-":A -= B; break;

case "×":A *= B; break;

case "÷":A /= B; break;

}

long C = Math.round(A);

a = b = Math.abs(C - A) < 1e-12 ? C + "" : A + "";

comp.setText(b);

}

else if (s == "±" || s == "√" || s == "x²" || s == "1/x") {

double B = Double.parseDouble(b);

switch (s) {

case "±":B = -B; break;

case "√":B = Math.sqrt(B); break;

case "x²":B *= B; break;

case "1/x":B = 1 / B; break;

}

long C = Math.round(B);

b = Math.abs(C - B) < 1e-12 ? C + "" : B + "";

comp.setText(b);

}

else if (s == "←") {

if (b.length() != 0) {

b = b.substring(0, b.length() - 1);

comp.setText(b);

}

}

else if (s.charAt(0) == 'C') {

a = b = op = "";

comp.setText("0");

}

}

}

public static void main(String[] args) {

EventQueue.invokeLater(()-> {

JFrame frame = new Calculator();

frame.setTitle("Calculator");

frame.setResizable(false);

frame.setLocationRelativeTo(null);

frame.setSize(297, 390);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

});

}

}

效果图

993f4a94682ace963cc7c71f3ca05923.png

按键顺序参照Win10计算器:

2650fb9066da94df450a096317b2016c.png

ps:除了%不知道有啥用,还有CE和C的功能写的一样之外,其他都实现了

暂时还没有发现逻辑错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值