| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2272 人关注过本帖
标题:java简易计算器,不知道哪里的错误,最后计算结果都是0.0
只看楼主 加入收藏
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:3 
java简易计算器,不知道哪里的错误,最后计算结果都是0.0
package keshihua;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Ksh11 extends JFrame implements ActionListener
{
static JPanel pan=new JPanel();
static JTextField tf=new JTextField("");
static JButton[] b=new JButton[10];
static JButton bc,bm,bd,ba,be,bdot;
    public static void main(String[] args)
    {
        Ksh11 frm=new Ksh11();
        for(int i=0;i<=9;i++)
        {
            b[i]=new JButton(""+i);
        }
        bdot=new JButton(" . ");    ba=new JButton("+");    bd=new JButton("-");
        bm=new JButton("*");    bc=new JButton("/");     be=new JButton("=");
        frm.setTitle("网格式布局管理器 Gridlayout");
        frm.setLayout(null);
        frm.setSize(300, 220);
        frm.setResizable(true);
        GridLayout grid=new GridLayout(4,4);
        tf.setBounds(35, 15, 200, 25);
        pan.setBounds(35, 50, 200, 130);
        tf.setBackground(Color.CYAN);
        tf.setHorizontalAlignment(JTextField.RIGHT);
        pan.add(b[7]);    pan.add(b[8]);    pan.add(b[9]);    pan.add(bc);
        pan.add(b[4]);    pan.add(b[5]);    pan.add(b[6]);    pan.add(bm);
        pan.add(b[1]);    pan.add(b[2]);    pan.add(b[3]);    pan.add(bd);
        pan.add(b[0]);    pan.add(bdot);  pan.add(be);    pan.add(ba);
        
        b[0].addActionListener(frm);
        b[1].addActionListener(frm);
        b[2].addActionListener(frm);
        b[3].addActionListener(frm);
        b[4].addActionListener(frm);
        b[5].addActionListener(frm);
        b[6].addActionListener(frm);
        b[7].addActionListener(frm);
        b[8].addActionListener(frm);
        b[9].addActionListener(frm);
        bc.addActionListener(frm);
        bm.addActionListener(frm);
        bd.addActionListener(frm);
        ba.addActionListener(frm);
        be.addActionListener(frm);
        bdot.addActionListener(frm);
        
        frm.add(tf);
        frm.add(pan);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
   
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
   
                int bz=0;
                double flag1=0,flag2=0,flag3=0;
                 if(e.getSource()==bdot){
                    tf.setText("0.");
                }
                else if(e.getSource()==b[0]){
                    tf.setText(tf.getText()+"0");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[1]){
                    tf.setText(tf.getText()+"1");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[2]){
                    tf.setText(tf.getText()+"2");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[3]){
                    tf.setText(tf.getText()+"3");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[4]){
                    tf.setText(tf.getText()+"4");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[5]){
                    tf.setText(tf.getText()+"5");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[6]){
                    tf.setText(tf.getText()+"6");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[7]){
                    tf.setText(tf.getText()+"7");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[8]){
                    tf.setText(tf.getText()+"8");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[9]){
                    tf.setText(tf.getText()+"9");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==ba){
                    tf.setText(tf.getText()+"+");
                    flag1=flag2;
                    bz=0;
                }
                else if(e.getSource()==bd){
                    tf.setText(tf.getText()+"-");
                    flag1=flag2;
                    bz=1;
                }
                else if(e.getSource()==bm){
                    tf.setText(tf.getText()+"*");
                    flag1=flag2;
                    bz=2;
                }
                else if(e.getSource()==bc){
                    tf.setText(tf.getText()+"/");
                    flag1=flag2;
                    bz=3;
                }
                else if(e.getSource()==be){
                    if(bz==0){
                        flag3=flag1+flag2;
                    }
                    else if(bz==1){
                        flag3=flag1-flag2;
                    }
                    else if(bz==2){
                        flag3=flag1*flag2;
                    }
                    else if(bz==3){
                        flag3=flag1/flag2;
                    }
                    tf.setText(tf.getText()+"="+flag3);
                    System.out.println(flag3);
                    System.out.println(flag2);
                    System.out.println(flag1);
                }
               
            }
        
}
搜索更多相关主题的帖子: 计算器 package public import java 
2017-03-28 12:26
纵横阳仔
Rank: 7Rank: 7Rank: 7
来 自:自学成才的火星人儿
等 级:贵宾
威 望:26
帖 子:147
专家分:698
注 册:2016-10-20
收藏
得分:10 
我建议你可以使用断点进行一步一步的查看
2017-04-01 19:58
qq3910004
Rank: 2
来 自:呼伦贝尔
等 级:论坛游民
威 望:1
帖 子:3
专家分:20
注 册:2017-4-4
收藏
得分:10 
同上,Debug一下啊

谁不是菜鸟过来的?我敢确定你知道的技术根本不是什么惊天秘密!所以我们来分享吧!
2017-04-04 11:03
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
收藏
得分:0 
虽然弄好了  but还是搞不懂为毛上边的会不行……


package keshihua;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Ksh11 extends JFrame
{
   
    static JPanel pan=new JPanel();
    static JTextField tf=new JTextField(30);
    static JButton[] b=new JButton[10];
    static JButton bc,bm,bd,ba,be,bdot;
    public static void main(String[] args)
    {
        JFrame frm=new JFrame();
        for(int i=0;i<=9;i++)
        {
            b[i]=new JButton(""+i);
        }
        bdot=new JButton(" . ");    ba=new JButton("+");    bd=new JButton("-");
        bm=new JButton("*");    bc=new JButton("/");     be=new JButton("=");
        frm.setTitle("网格式布局管理器 Gridlayout");
        frm.setLayout(null);
        frm.setSize(300, 220);
        frm.setResizable(true);
        GridLayout grid=new GridLayout(4,4);
        tf.setBounds(35, 15, 200, 25);
        pan.setBounds(35, 50, 200, 130);
        tf.setBackground(Color.CYAN);
        tf.setHorizontalAlignment(JTextField.RIGHT);
        pan.add(b[7]);    pan.add(b[8]);    pan.add(b[9]);    pan.add(bc);
        pan.add(b[4]);    pan.add(b[5]);    pan.add(b[6]);    pan.add(bm);
        pan.add(b[1]);    pan.add(b[2]);    pan.add(b[3]);    pan.add(bd);
        pan.add(b[0]);    pan.add(bdot);  pan.add(be);    pan.add(ba);
        Handle h=new Handle();
        b[0].addActionListener(h);
        b[1].addActionListener(h);
        b[2].addActionListener(h);
        b[3].addActionListener(h);
        b[4].addActionListener(h);
        b[5].addActionListener(h);
        b[6].addActionListener(h);
        b[7].addActionListener(h);
        b[8].addActionListener(h);
        b[9].addActionListener(h);
        bc.addActionListener(h);
        bm.addActionListener(h);
        bd.addActionListener(h);
        ba.addActionListener(h);
        be.addActionListener(h);
        bdot.addActionListener(h);
        
        frm.add(tf);
        frm.add(pan);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
    static class Handle implements ActionListener{
        double flag1=0,flag2=0,flag3=0;
        int bz=0;
    public void actionPerformed(ActionEvent e) {
            
                 if(e.getSource()==bdot){
                    tf.setText(".");
                }
                else if(e.getSource()==b[0]){
                    tf.setText(tf.getText()+"0");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[1]){
                    tf.setText(tf.getText()+"1");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[2]){
                    tf.setText(tf.getText()+"2");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[3]){
                    tf.setText(tf.getText()+"3");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[4]){
                    tf.setText(tf.getText()+"4");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[5]){
                    tf.setText(tf.getText()+"5");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[6]){
                    tf.setText(tf.getText()+"6");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[7]){
                    tf.setText(tf.getText()+"7");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[8]){
                    tf.setText(tf.getText()+"8");
                    flag2=Double.parseDouble(tf.getText());
                }
                else if(e.getSource()==b[9]){
                    tf.setText(tf.getText()+"9");
                    flag2=Double.parseDouble(tf.getText());
                    
                }
                else if(e.getSource()==ba){
                    tf.setText("");
                    flag1=flag2;
                    bz=0;
                    
                }
                else if(e.getSource()==bd){
                    tf.setText("");
                    flag1=flag2;
                    bz=1;
                    
                }
                else if(e.getSource()==bm){
                    tf.setText("");
                    flag1=flag2;
                    bz=2;
                }
                else if(e.getSource()==bc){
                    tf.setText("");
                    flag1=flag2;
                    bz=3;
                }
                else if(e.getSource()==be)
                {
                    
                    if(bz==0){
                        flag3=flag1+flag2;
                    }
                    else if(bz==1){
                        flag3=flag1-flag2;
                    }
                    else if(bz==2){
                        flag3=flag1*flag2;
                    }
                    else if(bz==3){
                        flag3=flag1/flag2;
                    }
                    tf.setText(flag3+"");
                    
                }
               
    }
    }
        
}

唯望君安~
2017-04-04 19:55
快速回复:java简易计算器,不知道哪里的错误,最后计算结果都是0.0
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063101 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved