一个简单计算器java程序,不知道为什么除法总是算错

问题描述

一个简单计算器java程序,不知道为什么除法总是算错

一个简单计算器java程序,不知道为什么除法总是算错(不是全概率是高概率应该说,麻烦大大帮忙看看,非常感激

 package com.calculator;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Component;
import java.awt.Point;

public class Calculator_ extends JFrame {

    private JPanel contentPane;
    private JTextField tf;
    private JButton b7;
    private JButton b8;
    private JButton b9;
    private JButton b4;
    private JButton b5;
    private JButton b6;
    private JPanel panel_1;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b0;
    private JButton bpoint;
    private JButton badd;
    private JButton bcut;
    private JButton bmul;
    private JButton bdiv;
    private JButton bresult;
    private JButton bempty;
    StringBuilder sb=new StringBuilder("");
    StringBuilder sb2=new StringBuilder("");
    char c;
    double d;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calculator_ frame = new Calculator_();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Calculator_() {
        setTitle("calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 384, 279);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 432, 55);
        contentPane.add(panel);
        panel.setLayout(new BorderLayout(0, 0));

        tf = new JTextField();
        tf.setEditable(false);
        panel.add(tf);
        tf.setColumns(10);

        panel_1 = new JPanel();
        panel_1.setBounds(436, 53, -434, 260);
        contentPane.add(panel_1);

        b7 = new JButton("7");
        b7.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("7");
                tf.setText(sb.toString());
            }
        });
        b7.setBounds(0, 53, 80, 46);
        contentPane.add(b7);

        b8 = new JButton("8");
        b8.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("8");
                tf.setText(sb.toString());
            }
        });
        b8.setBounds(78, 53, 80, 46);
        contentPane.add(b8);

        b9 = new JButton("9");
        b9.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                sb.append("9");
                tf.setText(sb.toString());
            }
        });
        b9.setBounds(158, 53, 80, 46);
        contentPane.add(b9);

        b4 = new JButton("4");
        b4.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("4");
                tf.setText(sb.toString());
            }
        });
        b4.setBounds(0, 97, 80, 46);
        contentPane.add(b4);

        b5 = new JButton("5");
        b5.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("5");
                tf.setText(sb.toString());
            }
        });
        b5.setBounds(78, 97, 80, 46);
        contentPane.add(b5);

        b6 = new JButton("6");
        b6.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                sb.append("6");
                tf.setText(sb.toString());
            }
        });
        b6.setBounds(158, 97, 80, 46);
        contentPane.add(b6);

        b1 = new JButton("1");
        b1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("1");
                tf.setText(sb.toString());
            }
        });
        b1.setBounds(0, 144, 80, 46);
        contentPane.add(b1);

        b2 = new JButton("2");
        b2.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("2");
                tf.setText(sb.toString());
            }
        });
        b2.setBounds(78, 144, 80, 46);
        contentPane.add(b2);

        b3 = new JButton("3");
        b3.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("3");
                tf.setText(sb.toString());
            }
        });
        b3.setBounds(158, 144, 80, 46);
        contentPane.add(b3);

        b0 = new JButton("0");
        b0.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append("0");
                tf.setText(sb.toString());
            }
        });
        b0.setBounds(0, 189, 158, 46);
        contentPane.add(b0);

        bpoint = new JButton(".");
        bpoint.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb.append(".");
                tf.setText(sb.toString());
            }
        });
        bpoint.setBounds(158, 189, 80, 46);
        contentPane.add(bpoint);

        badd = new JButton("+");
        badd.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb2=sb;
                sb=new StringBuilder("");
                c='+';
                tf.setText("");
            }
        });
        badd.setBounds(236, 97, 66, 46);
        contentPane.add(badd);

        bcut = new JButton("-");
        bcut.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb2=sb;
                sb=new StringBuilder("");
                c='-';
                tf.setText("");
            }
        });
        bcut.setBounds(236, 144, 66, 46);
        contentPane.add(bcut);

        bmul = new JButton("*");
        bmul.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb2=sb;
                sb=new StringBuilder("");
                c='*';
                tf.setText("");
            }
        });
        bmul.setBounds(301, 97, 66, 46);
        contentPane.add(bmul);

        bdiv = new JButton("/");
        bdiv.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb2=sb;
                sb=new StringBuilder("");
                c='/';
                tf.setText("");
            }
        });
        bdiv.setBounds(301, 144, 66, 46);
        contentPane.add(bdiv);

        bempty = new JButton("empty");
        bempty.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                sb=new StringBuilder("");
                sb2=new StringBuilder("");
                tf.setText("");
            }
        });
        bempty.setBounds(236, 53, 131, 46);
        contentPane.add(bempty);

        bresult = new JButton("=");
        bresult.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                    try {
                        switch (c) {
                        case '+':
                            d = Double.parseDouble(sb.toString())
                                    + Double.parseDouble(sb2.toString());
                            break;
                        case '-':
                            d = Double.parseDouble(sb.toString())
                                    - Double.parseDouble(sb2.toString());
                            break;
                        case '*':
                            d = Double.parseDouble(sb.toString())
                                    * Double.parseDouble(sb2.toString());
                            break;
                        case '/':
                            d = Double.parseDouble(sb.toString().trim())
                                    / Double.parseDouble(sb2.toString().trim());
                            break;
                        }
                        tf.setText("" + d);
                    } catch (NumberFormatException e) {
                        tf.setText("输入数据格式出错,请清空重新输入");
                    }
            }
        });
        bresult.setBounds(236, 189, 131, 45);
        contentPane.add(bresult);
    }
}

解决方案

 try {
                        switch (c) {
                        case '+':
                            d = Double.parseDouble(sb.toString())
                                    + Double.parseDouble(sb2.toString());
                            break;
                        case '-':
                            d = Double.parseDouble(sb.toString())
                                    - Double.parseDouble(sb2.toString());
                            break;
                        case '*':
                            d = Double.parseDouble(sb.toString())
                                    * Double.parseDouble(sb2.toString());
                            break;
                        case '/':
                            d = Double.parseDouble(sb.toString().trim())
                                    / Double.parseDouble(sb2.toString().trim());
                            break;
                        }
                        tf.setText("" + d);
                    } catch (NumberFormatException e) {
                        tf.setText("输入数据格式出错,请清空重新输入");
                    }
减法和除法都反了
应该是sb2除以或者减去sb
case '-':
                            d = Double.parseDouble(sb2.toString());
                                    - Double.parseDouble(sb.toString())
                            break;
                        case '/':
                            d = Double.parseDouble(sb2.toString().trim())
                                    / Double.parseDouble(sb.toString().trim());
                            break;

解决方案二:

用BigDecimal做四则运算试试。
http://bbs.csdn.net/topics/390152581

解决方案三:

建议你参考这个源码

http://download.csdn.net/detail/li461125839/3425313

时间: 2025-01-02 15:55:09

一个简单计算器java程序,不知道为什么除法总是算错的相关文章

一个简单的java 程序 填空题 谢谢 各位了

问题描述 classmyclass{____inty=5;____voidshow(){y++;system.out,println("y="+y);}}publicclassusemyclass{publicstaticvoidmain(stringargs[]){_____;_____;}} 解决方案 解决方案二:1.private2.public3.myclassm=newmyclass()4.m.show()解决方案三:谢谢你们啦解决方案四:1.private2.public3

java代码-我用java写了一个简易计算器的程序,但是有时候结果不正确

问题描述 我用java写了一个简易计算器的程序,但是有时候结果不正确 就是有时候计算结果后面会显示很多9或者0 ,比如计算8.3-2.1时,请问大神怎么解决啊? 我是菜菜鸟,程序如下: //Calculator.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame { private JPanel p1=new JPanel();

Java核心技术卷I基础知识3.1 一个简单的Java应用程序

第3章 Java的基本程序设计结构 ▲  一个简单的Java应用程序     ▲  字符串 ▲  注释                      ▲  输入输出 ▲  数据类型               ▲  控制流 ▲  变量                      ▲  大数值 ▲  运算符                  ▲  数组   现在,假定已经成功地安装了JDK,并且能够运行第2章中给出的示例程序.我们从现在开始将介绍Java应用程序设计.本章主要介绍程序设计的基本概念(如数

用EclipseME0.5.5创建一个简单的J2ME程序

程序|创建 用EclipseME0.5.5创建一个简单的J2ME程序 上一篇文章<如何在Eclipse3.0.1上安装EclipseME 0.5.5版本>,我们已经讲了如何在Eclipse3.0.1上安装Eclipse程序. 今天我们就要用他来创建我们的第一个程序.绝大多数程序教程的第一个程序,都叫做HelloWorld.那么我们就来编一个HelloWorld程序. 1, 文件2, 新建项目3, 选择J2ME,J2ME Midlet Suite4, 输入项目名称:MyFirstJ2ME5, 选

一个简单的Java EE&amp;Docker示例

本文讲的是一个简单的Java EE&Docker示例,[编者的话]学习Docker的最好办法就是迅速在工作中应用它,本文作者使用Docker部署了一个Java EE应用,非常简单和方便.需要注意的是,由于作者写作时本地网络有问题,所以Dockerfile中很多的资源都没有从网络下载,你再实践时,可以尝试修改.学习快乐 :) 本文中,我们将会把Java EE和Docker结合,具体内容如下: 创建.构建并运行一个Docker镜像: 通过镜像启动一个Wildfly服务器,并部署了一个JavaEE示例

c-请大家帮我检查一个简单的C程序

问题描述 请大家帮我检查一个简单的C程序 代码如下:#include #include #include #define maxn 99struct student{ char name[20]; int grade;} stu[maxn];int cmp(const void*a const void*b){ return ((struct student*)a)->grade - ((struct student*)b)->grade;}int main(void){ puts("

ANTLR#1:描述一个简单计算器

ANTLR是什么鬼?引用官网的说明, What is ANTLR? ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and framewo

for-请教各位 一个简单的 java 小问题

问题描述 请教各位 一个简单的 java 小问题 int arr[]={1,2,3,4,5,6,7,8,9,10}; int i,temp; for(i=0;i<10;i++){ } System.out.println("a"+i); temp=arr[0]; for(i=0;i<9;i++){ arr[i]=arr[i+1]; } System.out.println("b"+i); System.out.println("c"+

java 自学 新手-一个简单的Java 计算问题

问题描述 一个简单的Java 计算问题 输出结果总是0,大牛们,可不可以告诉我错在哪里了!代码如下package HelloWorld;import java.util.Scanner;public class Tax { public static void main(String[] args) { double a = 0.0b=0.0;//定义工资为a个人所得税为b; System.out.println(""请输入您的月工资,我们将严密计算您的个人所得税!"&quo