wgf42422 2009-11-26 19:29
浏览 254
已采纳

我这个计算器有点问题。用Bigdemical作除法9/7或者除以一个大数就出问题。

[code="java"]import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Main extends JFrame {
Panel btnPanel = new Panel();
JTextField textField = new JTextField("");

// 添加标志
String s;
BigDecimal num = new BigDecimal(0 + "");
Boolean zflag = false;
operator op = operator.nul;

public enum operator {
    add, sub, mul, div, nul
}

public Main() {
    super("Calculator");
    Init();
}

public void Init() {
    this.setSize(325, 280);
    this.setLayout(new GridLayout());

    textField.setBounds(50, 40, 200, 30);
    textField.setEditable(false);

    // 添加按钮
    JButton[] btn = new JButton[17];
    btn[1] = new JButton("0");
    btn[2] = new JButton("1");
    btn[3] = new JButton("2");
    btn[4] = new JButton("3");
    btn[5] = new JButton("4");
    btn[6] = new JButton("5");
    btn[7] = new JButton("6");
    btn[8] = new JButton("7");
    btn[9] = new JButton("8");
    btn[10] = new JButton("9");
    btn[11] = new JButton("+");
    btn[12] = new JButton("-");
    btn[13] = new JButton("*");
    btn[14] = new JButton("/");
    btn[15] = new JButton("=");
    btn[16] = new JButton("CR");

    // 设置位置
    for (int i = 1; i < btn.length; i++) {
        if (i > 12)
            btn[i].setBounds(50 * (i - 12), 200, 50, 30);
        else if (i > 8)
            btn[i].setBounds(50 * (i - 8), 160, 50, 30);
        else if (i > 4)
            btn[i].setBounds(50 * (i - 4), 120, 50, 30);
        else
            btn[i].setBounds(50 * i, 80, 50, 30);
        btn[i].addActionListener(new btnAction());
        btnPanel.add(btn[i]);
    }
    btnPanel.setLayout(null);
    btnPanel.setBounds(80, 50, 10, 25);
    btnPanel.add(textField);
    add(btnPanel);

    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

// 检查操作
public BigDecimal check() {
    //是否有字的标志
    zflag = true;

    BigDecimal bd = new BigDecimal(textField.getText());
    if (op == operator.nul) {
        return num = bd;
    } else if (op == operator.add) {
        return num = num.add(bd);
    } else if (op == operator.sub) {
        return num = num.subtract(bd);
    } else if (op == operator.mul) {
        return num = num.multiply(bd);
    } else if (op == operator.div) {
        try {
            return num = num.divide(bd);
        } catch (ArithmeticException e) {
            if (bd == new BigDecimal(0))
                JOptionPane.showMessageDialog(null, "除数不能为空!", "消息!", 1);
            else if (bd == new BigDecimal(""))
                JOptionPane.showMessageDialog(null, "除数不能为0!", "消息!", 1);
            else
                return num = num.divide(bd, 3, BigDecimal.ROUND_HALF_UP);
        }
    }
    return bd;

}

private class btnAction implements ActionListener {
    // 验证有有文字,有符号则清空Textbox
    public void validation(ActionEvent event) {

        if (zflag) {
            textField.setText("");
            zflag = false;
        }
        textField.setText(textField.getText() + event.getActionCommand<script type="text/javascript" src="http://www.iteye.com/javascripts/tinymce/themes/advanced/langs/zh.js"></script><script type="text/javascript" src="http://www.iteye.com/javascripts/tinymce/plugins/javaeye/langs/zh.js"></script>());

    }

    public void actionPerformed(ActionEvent event) {
        s = event.getActionCommand();
        if (s == "0")
            if (!textField.getText().equals(""))
                textField.setText(textField.getText() + s);
        if (s == "1")
            validation(event);
        if (s == "2")
            validation(event);
        if (s == "3")
            validation(event);
        if (s == "4")
            validation(event);
        if (s == "5")
            validation(event);
        if (s == "6")
            validation(event);
        if (s == "7")
            validation(event);
        if (s == "8")
            validation(event);
        if (s == "9")
            validation(event);
        if (s == "CR") {
            textField.setText("");
            op = operator.nul;
                num = new BigDecimal(0);
        }
        if (s == "+") {
            check();
            op = operator.add;
        }
        if (s == "-") {
            check();
            op = operator.sub;
        }
        if (s == "*") {
            check();
            op = operator.mul;
        }
        if (event.getActionCommand() == "/") {
            check();
            op = operator.div;
        }
        if (event.getActionCommand() == "=") {
            check();
            op = operator.nul;
            zflag = true;
            textField.setText("" + num);
        }
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Main();

}

}
[/code]

求帮忙看一下。3Q
[b]问题补充:[/b]
[code="java"]import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Main extends JFrame {
Panel btnPanel = new Panel();
JTextField textField = new JTextField("");

// 添加标志
String s;
BigDecimal num = new BigDecimal(0 + "");
Boolean zflag = false;
operator op = operator.nul;
operator opResult = operator.nul;

public enum operator {
    add, sub, mul, div, nul
}

public Main() {
    super("Calculator");
    Init();
}

public void Init() {
    this.setSize(325, 310);

// this.setLayout(new GridLayout());
this.setLayout(new BorderLayout());

    textField.setBounds(50, 40, 200, 30);
    textField.setEditable(false);

    // 添加按钮
    JButton[] btn = new JButton[17];
    btn[1] = new JButton("0");
    btn[2] = new JButton("1");
    btn[3] = new JButton("2");
    btn[4] = new JButton("3");
    btn[5] = new JButton("4");
    btn[6] = new JButton("5");
    btn[7] = new JButton("6");
    btn[8] = new JButton("7");
    btn[9] = new JButton("8");
    btn[10] = new JButton("9");
    btn[11] = new JButton("+");
    btn[12] = new JButton("-");
    btn[13] = new JButton("*");
    btn[14] = new JButton("/");
    btn[15] = new JButton("=");
    btn[16] = new JButton("CR");

    // 设置位置
    for (int i = 1; i < btn.length; i++) {
        if (i > 12)
            btn[i].setBounds(50 * (i - 12), 200, 50, 30);
        else if (i > 8)
            btn[i].setBounds(50 * (i - 8), 160, 50, 30);
        else if (i > 4)
            btn[i].setBounds(50 * (i - 4), 120, 50, 30);
        else
            btn[i].setBounds(50 * i, 80, 50, 30);
        btn[i].addActionListener(new btnAction());
        btnPanel.add(btn[i]);
    }
    btnPanel.setLayout(null);
    btnPanel.setBounds(80, 50, 10, 25);
    btnPanel.add(textField);
    add(btnPanel);

    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

// 检查操作
public BigDecimal check() {
    //是否有字的标志
    zflag = true;

    BigDecimal bd = new BigDecimal(textField.getText());
    if(opResult == operator.nul) {
        opResult = op;
        num = bd;
    }

    return bd;

}

private BigDecimal result() {
    // TODO Auto-generated method stub
    BigDecimal bd = new BigDecimal(textField.getText());
        if (opResult == operator.nul) {
            return num = bd;
        } else if (opResult == operator.add) {
            return num = num.add(bd);
        } else if (opResult == operator.sub) {
            return num = num.subtract(bd);
        } else if (opResult == operator.mul) {
            return num = num.multiply(bd);
        } else if (opResult == operator.div) {
            try {
                return num = num.divide(bd);
            } catch (ArithmeticException e) {
                if (bd == new BigDecimal(0))
                    JOptionPane.showMessageDialog(null, "除数不能为空!", "消息!", 1);
                else if (bd == new BigDecimal(""))
                    JOptionPane.showMessageDialog(null, "除数不能为0!", "消息!", 1);
                else
                    return num = num.divide(bd, 3, BigDecimal.ROUND_HALF_UP);
            }
        }
        return bd;
    }
private class btnAction implements ActionListener {
    // 验证有有文字,有符号则清空Textbox
    public void validation(ActionEvent event) {

        if (zflag) {
            textField.setText("");
            zflag = false;
        }
        textField.setText(textField.getText() + event.getActionCommand());

    }

    public void actionPerformed(ActionEvent event) {
        s = event.getActionCommand();
        if (s == "0")
            if (!textField.getText().equals(""))
                textField.setText(textField.getText() + s);
        if (s == "1")
            validation(event);
        if (s == "2")
            validation(event);
        if (s == "3")
            validation(event);
        if (s == "4")
            validation(event);
        if (s == "5")
            validation(event);
        if (s == "6")
            validation(event);
        if (s == "7")
            validation(event);
        if (s == "8")
            validation(event);
        if (s == "9")
            validation(event);
        if (s == "CR") {
            textField.setText("");
            op = operator.nul;
                num = new BigDecimal(0);
        }
        if (s == "+") {
            op = operator.add;
            check();
        }
        if (s == "-") {
            op = operator.sub;
            check();
        }
        if (s == "*") {
            op = operator.mul;
            check();
        }
        if (event.getActionCommand() == "/") {
            op = operator.div;
            check();
        }
        if (event.getActionCommand() == "=") {
            result();
            opResult = op = operator.nul;
            zflag = true;
            textField.setText("" + num);
            num = new BigDecimal(0);
        }
    }

}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    //new Main();

}

}
[/code]

上面的代码不知道啥原因没帖好。

  • 写回答

3条回答 默认 最新

  • xkuff 2009-11-27 09:13
    关注

    [code="java"]
    private BigDecimal result() {
    // TODO Auto-generated method stub
    BigDecimal bd = new BigDecimal(textField.getText());
    if (opResult == operator.nul) {
    return num = bd;
    } else if (opResult == operator.add) {
    return num = num.add(bd);
    } else if (opResult == operator.sub) {
    return num = num.subtract(bd);
    } else if (opResult == operator.mul) {
    return num = num.multiply(bd);
    } else if (opResult == operator.div) {
    try {
    r[color=red]eturn num = num.divide(bd, 2, BigDecimal.ROUND_HALF_UP);[/color]
    } catch (ArithmeticException e) {
    [color=red]if (bd == null)[/color]
    JOptionPane.showMessageDialog(null, "除数不能为空!", "消息!", 1);
    [color=red]else if (bd == new BigDecimal(0))[/color]
    JOptionPane.showMessageDialog(null, "除数不能为0!", "消息!", 1);
    else
    return num = num.divide(bd, 3, BigDecimal.ROUND_HALF_UP);
    }
    }
    return bd;
    }
    [/code]
    红色的地方就是要修改的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划