标题:请帮忙看下,为什么这个计算器不能运算呢?
只看楼主
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
结帖率:84.21%
已结贴  问题点数:20 回复次数:2 
请帮忙看下,为什么这个计算器不能运算呢?
/**
 * @(#)SheJi.java
 *
 *
 * @author 张愉
 * @version 1.00 2014/6/22
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SheJi extends JFrame implements ActionListener{
    private    JButton b[] = new JButton[28];
    private    JTextField t;
    private    JPanel p[]=new JPanel[10];
    private final String[] keys={"MC","MR","MS","M+","M-","back","CE","C","+/-","sqrt","7","8","9","/","%","4","5","6",
                                "*","1/x","1","2","3","-","=","0",".","+"};
                                
    public SheJi(){
        super("计算器--张愉");
        for(int i=0;i<keys.length;i++){
            b[i]=new JButton(keys[i]);
            b[i].setFont(new Font("黑体", Font.PLAIN, 15));
        }
        t=new JTextField(44);
        t.setFont(new Font("黑体", Font.PLAIN, 15));
        t.setText("0");
        t.setHorizontalAlignment(t.RIGHT);//文本在文本框里的对齐方式。
        t.setEditable(false);
        p[0]=new JPanel();//设置文本域所在的面板。
        p[0].add(t);
        p[1]=new JPanel();//设置前4行5列的按钮所在的面板。
        p[1].setLayout(new GridLayout(4,5,2,2));
        for(int i=0;i<20;i++){
            b[i].setPreferredSize(new Dimension(70,30));//强制设置按钮的大小。
            p[1].add(b[i]);        
        }
            
        p[2]=new JPanel(new BorderLayout(2,2)); //设置后两行的按钮所在的面板。
        p[3]=new JPanel();
        p[3].setLayout(new GridLayout(2,1,2,2));//设置1,2,0按钮所在的面板
        p[9]=new JPanel(new GridLayout(1,2,2,2));//设置1,2按钮所在的面板
        b[20].setPreferredSize(new Dimension(70,30));
           p[9].add(b[20]);
        b[21].setPreferredSize(new Dimension(70,30));
          p[9].add(b[21]);
        //把1,2按钮所在的面板和0按钮所在的面板添加到1,2,0添加到p[3]面板
        p[3].add(p[9]);
        b[25].setPreferredSize(new Dimension(50,20));
        p[3].add(b[25]);
        
        p[6]=new JPanel();
        p[6].setLayout(new GridLayout(2,1,2,2));
        b[22].setPreferredSize(new Dimension(70,30));
        p[6].add(b[22]);
        b[26].setPreferredSize(new Dimension(70,30));
        p[6].add(b[26]);
        
        p[4]=new JPanel();
        p[4].setLayout(new GridLayout(1,2,2,2));
        p[5]=new JPanel(new GridLayout(2,1,2,2));
        b[23].setPreferredSize(new Dimension(70,30));
        p[5].add(b[23]);
        b[27].setPreferredSize(new Dimension(70,30));
        p[5].add(b[27]);
        b[24].setPreferredSize(new Dimension(70,30));
        p[4].add(p[5]);
        p[4].add(b[24]);
        
   
        p[2].add("West",p[3]);
        p[2].add("East",p[4]);
        p[2].add("Center",p[6]);
        p[8]=new JPanel();
        p[8].add(p[0]);
        p[8].add(p[1]);
        p[8].add(p[2]);
        add(p[8]);
        
        //为各个按钮添加事件监听器。
        for(int i=0;i<keys.length;i++)
            b[i].addActionListener(this);     
        setSize(400,275);
        setVisible(true);
        setLocation(500,350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 
    }
    public void actionPerformed(ActionEvent e){
            double flag1=0,flag2=0,flag3=0;//前两个是为了记录操作的数字,第三个是结果
            int a=0;//标志运算的
            if(e.getSource()==b[6])//按钮CE/C清0功能
                t.setText("0");
            if(e.getSource()==b[26]){//按钮小数点(.)
                    if(t.getText().indexOf(".")==-1)//前面有小数点,indexOf()函数是查找字符串里面是否有字母,为了查看前面输入的字符是否有小数点
                    {
                        if(t.getText().equals("0"))
                            t.setText("0.");   
                        else
                          t.setText(t.getText()+".");
                    }
                    else
                        t.setText(t.getText());        
                }
            if(e.getSource()==b[7])//按钮C清0功能.
                t.setText("0");
            if(e.getSource()==b[25])//按钮0.
            {
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"0");
                }
                else{
                    t.setText(t.getText()+"0");
                }
                flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型。
            }
               
             if(e.getSource()==b[20]){//按钮1.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"1");
                }
                else{
                    t.setText(t.getText()+"1");
                }
                flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型。   
            }
             if(e.getSource()==b[21]){//按钮2.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"2");
                }
                else{
                    t.setText(t.getText()+"2");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型。
            }
            if(e.getSource()==b[22]){//按钮3.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"3");
                }
                else{
                    t.setText(t.getText()+"3");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }   
            if(e.getSource()==b[15]){//按钮4.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"4");
                }
                else{
                    t.setText(t.getText()+"4");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }   
             if(e.getSource()==b[16]){//按钮5.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"5");
                }
                else{
                    t.setText(t.getText()+"5");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }   
          if(e.getSource()==b[17]){//按钮6.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"6");
                }
                else{
                    t.setText(t.getText()+"6");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }
         if(e.getSource()==b[10]){//按钮7.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"7");
                }
                else{
                    t.setText(t.getText()+"7");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }
         if(e.getSource()==b[11]){//按钮8.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"8");
                }
                else{
                    t.setText(t.getText()+"8");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
            }
         if(e.getSource()==b[12]){//按钮9.
                if(t.getText().equals("0")){
                  t.setText("");   
                  t.setText(t.getText()+"9");
                }
                else{
                    t.setText(t.getText()+"9");
                }
                 flag1=Double.parseDouble(t.getText());//把文本强制转化为Double类型
               
            }
        if(e.getSource()==b[27]){//加法
            t.setText(t.getText()+"+");
            flag2=flag1;
            a=1;
        }
       else if(e.getSource()==b[23]){//减法
            t.setText(t.getText()+"-");
            flag2=flag1;
            a=2;
        }
        else if(e.getSource()==b[18]){//乘法
            t.setText(t.getText()+"*");
            flag2=flag1;
            a=3;
        }
        else if(e.getSource()==b[13]){//除法
            t.setText(t.getText()+"/");
            flag2=flag1;
            a=4;
        }
        else if(e.getSource()==b[14]){//百分数
            t.setText(t.getText());
            flag2=flag1;
            a=5;
        }
        else if(e.getSource()==b[19]){//倒数
            t.setText(t.getText());
            flag3=flag1;
            System.out.print(flag3);
            if(flag3==0)
                t.setText("0不能做除数");
            else
            {
                flag3=1/flag3;
                t.setText(flag3+"");
            }
               
        }
        else if(e.getSource()==b[9]){//平方数
            flag3=Math.sqrt(flag1);
            t.setText(flag3+"");//把double类型的文本转化为字符串。
        }
        if(e.getSource()==b[24]){//等号按钮并且根据a的值确定运算符号。
            if(a==1){//加法运算
             flag3=flag1+flag2;//flag2是第一次输入的数据,flag1是第二次输入的数据
             t.setText(flag3+"");//把double类型的文本转化为字符串   
            }
            
        }
        
    }
    public static void main(String[] args){
        new SheJi();
    }              
}
请帮忙看下代码!为什么不能进行运算呢?加法,平方,倒数都不能
搜索更多相关主题的帖子: 计算器 version private import 
2014-06-24 14:59
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
得分:20 
没有报错信息?

异常处理都没有,你怎么知道哪里错了

思考赐予新生,时间在于定义
2014-06-25 01:56
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
得分:0 
回复 2 楼 love 云彩
编译没有错误但是运行时出错了,已经解决了。
2014-06-25 09:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-433310-1-1.html




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

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