jubi_ 2015-06-21 11:11 采纳率: 0%
浏览 1793
已采纳

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 2015-06-21 11: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吧
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧