当前位置: 代码迷 >> Java相关 >> 我在做课设~这个计算器代码怎么总有问题啊!
  详细解决方案

我在做课设~这个计算器代码怎么总有问题啊!

热度:326   发布时间:2012-12-21 18:40:19.0
我在做课设~这个计算器代码怎么总有问题啊!
这个计算器要求把计算的结果保存在一个“.txt”文档中!计算器功能都好用!就是保存功能出错!各位好心人帮帮我吧!
//Calculator.java

public class Calculator{
private String result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

private double stringToDouble(String x){
double y = Double.parseDouble(x);
return y;
}
private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}else{
result = "除数不能为零!!";
}
break;
}
}

public String opAdd(String x){
operate(x);
op = add;
return result;
}
public String opSubtract(String x){
operate(x);
op = sub;
return result;
}
public String opMultiply(String x){
operate(x);
op = mul;
return result;
}
public String opDivide(String x){
operate(x);
op = div;
return result;
}
public String opEquals(String x){
operate(x);
op = 0;
return result;
}
public void opClean(){
op = 0;
result = "0";
}
}




//-------------------------------------------------------------------

//第二个
//CalculatorGUI.java

import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import java.io.*;

public class CalculatorGUI{
private Frame f;
private Panel p1,p2,p3;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal,bsave;
private TextField tf;
private String s,op;
private Calculator cal = new Calculator();
private boolean ifOp;
   

public CalculatorGUI(){
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();

b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
bsave = new Button("保存");

tf = new TextField(15);
tf.setEditable(false);


}

public void launchFrame(){
f.setSize(190,170);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.add(tf,BorderLayout.WEST);
p1.add(bsave,BorderLayout.CENTER);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,4));

b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
bsave.addActionListener(new setOperator_ActionListener());

p1.add(bsave);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);

//f.add(p3,BorderLayout.CENTER);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true);
}

public void setTextFieldText_Temp(){
if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){
tf.setText(tf.getText()+s);
}else{
tf.setText((tf.getText()+s).substring(0,15));
}
}
public void setTextFieldText(){
if(ifOp){
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
}else{
setTextFieldText_Temp();
}
}

public static void main(String[] args){
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}

class myWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}

class setOperator_ActionListener implements ActionListener{
   

public void actionPerformed(ActionEvent e){

   
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
else if(op.equals("保存")){

try
{ BufferedReader in = new BufferedReader(s);        
  PrintWriter   out= new PrintWriter(new BufferedWriter(new FileWriter("1.txt")));
                out.close();               
                in.close();
                }catch(IOException exc){
                    //eee.getStackTrace();
                }
}
}
}
}

好像就是这里有问题!! 好心人啊!帮帮我吧!我都上火了!跪谢!!!
搜索更多相关的解决方案: return  operate  private  计算器  double  

----------------解决方案--------------------------------------------------------
保存的话是要指定完整路径的
如果没有1.txt这个文件 你首先还需要创建这个文件
本帖最近评分记录
2012-12-21 13:18:05
shellingford

等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
  得分:0 
写文件方法不对
程序代码:

            else if (op.equals("保存")) {
                PrintWriter out=null;
                try {
                    out = new PrintWriter(new BufferedWriter(
                            new FileWriter("1.txt")));
                    out.write(s);
                } catch (IOException e2) {
                    e2.printStackTrace();
                }finally{
                    if(out!=null){
                        out.close();
                    }
                }
            }
本帖最近评分记录
2012-12-21 13:46:00
永远在一起楠

等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-21
  得分:0 
回复 2楼 hhwz
对了对了!谢谢你啊!
还有为什么这个计算器计算时:1.2/3不等于0.4!而是等于0.39999999997啊?

[ 本帖最后由 永远在一起楠 于 2012-12-22 13:38 编辑 ]
----------------解决方案--------------------------------------------------------
回复 3楼 shellingford
对了对了!谢谢你啊!
还有为什么这个计算器计算时:1.2/3不等于0.4!而是等于0.39999999997啊?


----------------解决方案--------------------------------------------------------
回复 5楼 永远在一起楠
因为double类型
也就是说存在这样一种情况,0到±Float.MIN_VALUE之间的值float类型无法表示,0 到±Double.MIN_VALUE之间的值double类型无法表示,这并没有什么好奇怪的,因为这些范围内的数值超出了它们的精度范围。
double不能表示所有的数据,典型的就是double 1.0-0.9  得到的是0.09999999999999998

如果程序想要避免这个可以选择四舍五入到一定精度,例如小数点后4位之类的


本帖最近评分记录
2012-12-22 09:09:26
永远在一起楠

等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-21
  得分:0 
回复 6楼 shellingford
我试试!真是太感谢你了!!!!
----------------解决方案--------------------------------------------------------
回复 6楼 shellingford
我改了!没改出来!
----------------解决方案--------------------------------------------------------
回复 6楼 shellingford
版主!好人!你在帮我改一下呗!我改了一天了!没明白怎么弄!
----------------解决方案--------------------------------------------------------
回复 6楼 shellingford
好人!帮帮忙!我是初学!不太会!
----------------解决方案--------------------------------------------------------
  相关解决方案