java写了个简易计算器,有个错误,希望大神指点!

小hua 发布于 2016/12/27 20:59
阅读 691
收藏 4

解读下一代网络:算力网络正从理想照进现实!>>>

写了一个简易计算器,代码如下,里边有个错误,当输入0.7的时候会变成0.7000000000001,不知道是什么原因,希望大神给指点!


package com.calculator;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class JCalculator extends WindowAdapter implements ActionListener {
private double result = 0;
private double data1 = 0;
private double radixPointDepth = 1;
private boolean radixPointIndicate = false;
private boolean resultIndicate = false;
private char prec = '+';
private Frame f;
private TextField tf;
private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18;
private Panel p;
private Panel p2;


static public void main(String args[]) {
JCalculator de = new JCalculator();
de.go();
}


public void go() {
f = new Frame("计算器");
p = new Panel();
p2 = new Panel();
p.setLayout(new GridLayout(4,4));
p2.setLayout(new GridLayout(1, 1));
tf = new TextField(30);
b1 = new Button("7");
b2 = new Button("8");
b3 = new Button("9");
b4 = new Button("+");
b5 = new Button("4");
b6 = new Button("5");
b7 = new Button("6");
b8 = new Button("-");
b9 = new Button("1");
b10 = new Button("2");
b11 = new Button("3");
b12 = new Button("*");
b13 = new Button("0");
b14 = new Button(".");
b15 = new Button("=");
b16 = new Button("/");
b17 = new Button("clear");
b18 = new Button("exit");
f.add(tf, "North");
f.add(p, "Center");
f.add(b18, "East");
f.add(p2, "South");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p2.add(b17);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
f.addWindowListener(this);
f.setSize(250, 190);
f.setVisible(true);
}


public void actionPerformed(ActionEvent e) {
String s;
s = e.getActionCommand();
switch (s.charAt(0)) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (resultIndicate) {
result = 0;
data1 = 0;
prec = '+';
}
Integer Int1 = new Integer(s);
if (radixPointIndicate) {
radixPointDepth = radixPointDepth / 10;
data1 = data1 + (Int1.intValue()) * radixPointDepth;

else {
data1 = data1 * 10 + (Int1.intValue());
}
Double displayNumber = new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate = false;
break;
case '+':
case '-':
case '*':
case '/':
case '=':
if (s.charAt(0) != '=' && resultIndicate) {
prec = s.charAt(0);
resultIndicate = false;
} else {
switch (prec) {
case '+':
result = result + data1;
break;
case '-':
result = result - data1;
break;
case '*':
result = result * data1;
break;
case '/':
result = result / data1;
break;
}
}
radixPointIndicate = false;
radixPointDepth = 1;
displayNumber = new Double(result);
tf.setText(displayNumber.toString());
if (s.charAt(0) != '=') {
data1 = 0;
prec = s.charAt(0);
} else {
resultIndicate = true;
}
break;
case '.':
radixPointIndicate = true;
break;
}
if (s.equals("clear")) {
result = 0;
data1 = 0;
radixPointDepth = 1;
radixPointIndicate = false;
resultIndicate = false;
tf.setText("");
}
if (s.equals("exit")) {
System.exit(0);
}
}


public void windowClosing(WindowEvent e) {
System.exit(0);
}
}



加载中
0
如比如比
如比如比
使用浮点数的原因,用BigDecimal实现下
如比如比
如比如比
回复 @小hua : 超过两百字,看引用吧!
小hua
小hua
是double的原因?
0
如比如比
如比如比

引用来自“离未罔两”的评论

使用浮点数的原因,用BigDecimal实现下
就现在的问题内容,没有别的了。要不就看看这个吧,https://my.oschina.net/dubenju/blog/543120
double a = 0.0 + 7 * 0.1;
DBGFloat.debugDouble(a);


0.7000000000000001的二进制存储0[01111111110][0110011001100110011001100110011001100110011001100111]=>1×2的-1次方×1.4000000000000001=0.7000000000000001

0
跬步至千里
使用double类型 就会这样
0
秋痕
秋痕
System.out.println(2.55 * 100);
返回顶部
顶部