2 jubi jubi_ 于 2015.06.21 19:11 提问

java简单计算器,计算结果总是出错
 package com.calculator;

import java.awt.*;


import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.event.*;

public class CALC extends JFrame {

    private JPanel jp;
    private JTextField tf;
    private JButton b7;
    private JButton b8;
    private JButton b9;
    private JButton bdiv;
    private JButton b4;
    private JButton b5;
    private JButton b6;
    private JButton bmul;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton bcut;
    private JButton b0;
    private JButton bpoint;
    private JButton bequal;
    private JButton badd;
    private JButton bexit;
    StringBuilder sb=new StringBuilder(""),
                  stb=new StringBuilder("");
    char c;
    double d=0;
    private JButton bc;
    private JButton bt;
    private JButton byu;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CALC frame = new CALC();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public CALC() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 365, 314);
        jp = new JPanel();
        jp.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(jp);
        jp.setLayout(null);

        tf = new JTextField();
        tf.setBounds(5, 5, 339, 48);
        jp.add(tf);
        tf.setColumns(10);

        JPanel pa = new JPanel();
        pa.setBounds(5, 54, 339, 215);
        jp.add(pa);
        pa.setLayout(new GridLayout(0, 4, 4, 0));

        b7 = new JButton("7");
        b7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("7");
                tf.setText(sb.toString());              
            }
        });
        pa.add(b7);

        b8 = new JButton("8");
        b8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("8");
                tf.setText(sb.toString());
            }
        });
        pa.add(b8);

        b9 = new JButton("9");
        b9.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("9");
                tf.setText(sb.toString());
            }
        });
        pa.add(b9);

        bdiv = new JButton("/");
        bdiv.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                c='/';
                tf.setText("/");
                stb=sb;
                sb=new StringBuilder("");
            }
        });
        pa.add(bdiv);

        b4 = new JButton("4");
        b4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("4");
                tf.setText(sb.toString());
            }
        });
        pa.add(b4);

        b5 = new JButton("5");
        b5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("5");
                tf.setText(sb.toString());
            }
        });
        pa.add(b5);

        b6 = new JButton("6");
        b6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("6");
                tf.setText(sb.toString());
            }
        });
        pa.add(b6);

        bmul = new JButton("*");
        bmul.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                c='*';
                tf.setText("*");
                stb=sb;
                sb=new StringBuilder("");
            }
        });
        pa.add(bmul);

        b1 = new JButton("1");
        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("1");
                tf.setText(sb.toString());
            }
        });
        pa.add(b1);

        b2 = new JButton("2");
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("2");
                tf.setText(sb.toString());
            }
        });
        pa.add(b2);

        b3 = new JButton("3");
        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("3");
                tf.setText(sb.toString());
            }
        });
        pa.add(b3);

        bcut = new JButton("-");
        bcut.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                c='-';
                tf.setText("-");
                stb=sb;
                sb=new StringBuilder("");
            }
        });
        pa.add(bcut);

        b0 = new JButton("0");
        b0.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append("0");
                tf.setText(sb.toString());
            }
        });
        pa.add(b0);

        bpoint = new JButton(".");
        bpoint.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.append(".");
                tf.setText(sb.toString());
            }
        });
        pa.add(bpoint);

        bequal = new JButton("=");
        bequal.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            try{
                switch(c){
                    case '+':d=Double.parseDouble(stb.toString().trim())
                            +Double.parseDouble(stb.toString().trim()); break;
                        case '-': d=Double.parseDouble(stb.toString().trim())
                                -Double.parseDouble(stb.toString().trim()); break;  
                        case '/': d=Double.parseDouble(stb.toString().trim())
                                /Double.parseDouble(stb.toString().trim()); break;
                        case '*': d=Double.parseDouble(stb.toString().trim())
                                *Double.parseDouble(stb.toString().trim()); break;
                        case '%': d=Double.parseDouble(stb.toString().trim())
                                %Double.parseDouble(stb.toString().trim()); break;
                }   
                tf.setText(""+d);
            } catch (NumberFormatException a) {
                tf.setText("输入数据格式出错,请清空重新输入");
            }           
            }
        });
        pa.add(bequal);

        badd = new JButton("+");
        badd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                c='+';
                tf.setText("+");
                stb=sb;
                sb=new StringBuilder("");
            }
        });
        pa.add(badd);

        bc = new JButton("clear");
        bc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb=new StringBuilder("");
                tf.setText("");
            }
        });
        pa.add(bc);

        bt = new JButton("←");
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sb.deleteCharAt(sb.length()-1);
                tf.setText(sb.toString());
            }
        });
        pa.add(bt);

        byu = new JButton("%");
        byu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                c='%';
                tf.setText("%");
                stb=sb;
                sb=new StringBuilder("");
            }
        });
        pa.add(byu);

        bexit = new JButton("Exit");
        bexit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        pa.add(bexit);

    }
}

3个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.21 19:53
已采纳
 switch(c){
                    case '+':d=Double.parseDouble(stb.toString().trim())
                            +Double.parseDouble(stb.toString().trim()); break;
                        case '-': d=Double.parseDouble(stb.toString().trim())
                                -Double.parseDouble(stb.toString().trim()); break;  
                        case '/': d=Double.parseDouble(stb.toString().trim())
                                /Double.parseDouble(stb.toString().trim()); break;
                        case '*': d=Double.parseDouble(stb.toString().trim())
                                *Double.parseDouble(stb.toString().trim()); break;
                        case '%': d=Double.parseDouble(stb.toString().trim())
                                %Double.parseDouble(stb.toString().trim()); break;
                }   
                                怎么都是自己除自己呢
                                Double.parseDouble(stb.toString().trim())
                                *Double.parseDouble(stb.toString().trim())
                                另一个 应该是sb不是stb吧
jubi_
jubi_ 已经解决,傻死了,多谢!!
2 年多之前 回复
jubi_
jubi_ 只是除法会错……这次…………
2 年多之前 回复
jubi_
jubi_ 不过结果还是不对……
2 年多之前 回复
devmiao
devmiao   Ds   Rxr 2015.06.21 19:15
jubi_
jubi_ 我自己也写过成功的,换了种监听而已细节都差不多不知道为什么不行了啊
2 年多之前 回复
ubuntulinuxjava
ubuntulinuxjava   2015.06.22 14:22
Csdn user default icon
上传中...
上传图片
插入图片