| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 920 人关注过本帖
标题:“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
取消只看楼主 加入收藏
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
结帖率:100%
收藏
 问题点数:0 回复次数:2 
“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyCounter extends JFrame implements ActionListener
{
    private JTextField text;
    private JButton[] buttons;
    
    private int symbol;             // symbol表示要进行的运算
     private int f;                  // f为点击符号前读入的数
    private int l;                  // l为点击符号后读入的数
    private int p;                  // p为当前读入的数字
    private boolean flg;            // flg表示是否已经输入符号
    private boolean point;          // 是否已经输入点
    private double result;          // result为计算结果
    
    
    public MyCounter()
    {
        super("计算器");
        this.setSize(200,300);
        this.setLocation(400,300);
        
        flg = false;
        point = false;
        
        
        text = new JTextField("0");
        text.setBackground(Color.cyan);
        text.setHorizontalAlignment(JTextField.RIGHT);    // 右对齐显示
        text.setEditable(false);                          //文本框不可编辑   
        this.add(text,"North");
        
        JPanel p1 = new JPanel(new GridLayout(4,4));
        buttons = new JButton[17];
        buttons[0] =  new JButton("7");
        buttons[1] =  new JButton("8");
        buttons[2] =  new JButton("9");
        buttons[3] =  new JButton("+");
        buttons[4] =  new JButton("4");
        buttons[5] =  new JButton("5");
        buttons[6] =  new JButton("6");
        buttons[7] =  new JButton("-");
        buttons[8] =  new JButton("1");
        buttons[9] =  new JButton("2");
        buttons[10] = new JButton("3");
        buttons[11] = new JButton("*");
        buttons[12] = new JButton("0");
        buttons[13] = new JButton(".");
        buttons[14] = new JButton("=");
        buttons[15] = new JButton("/");
        for(int i=0;i<16;i++)
        {
            p1.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        this.add(p1,"Center");
        
        JPanel p2 = new JPanel();
        buttons[16] = new JButton("清除");
        buttons[16].addActionListener(this);
        buttons[16].setBackground(Color.green);
        p2.add(buttons[16]);
        this.add(p2,"South");
        
        this.setVisible(true);    
    }
    
    
    
    public void actionPerformed(ActionEvent e)
    {
        // 置零
        if(e.getSource()==buttons[16])
        {
            text.setText("0");
        }
        
        // 读入数字并显示在文本框上
        if(e.getSource()==buttons[0]||e.getSource()==buttons[1]||e.getSource()==buttons[2]
        ||e.getSource()==buttons[4]||e.getSource()==buttons[5]||e.getSource()==buttons[6]
        ||e.getSource()==buttons[8]||e.getSource()==buttons[9]||e.getSource()==buttons[10]
        ||e.getSource()==buttons[12])
        {
           
             p = Integer.parseInt(text.getText() + e.getActionCommand());
               text.setText(String.valueOf(p));                 
            if(flg)
            {
                l = p;
            }
                    
        }
        
        /*if(e.getSource()==buttons[13])
        {
            if(point)
            {
                
            }
        }*/
        
        // 读入符号
        if(e.getSource()==buttons[3]||e.getSource()==buttons[7]||
        e.getSource()==buttons[11]||e.getSource()==buttons[15])
        {
            f = Integer.parseInt(text.getText());
            //设置符号
            if(e.getSource()==buttons[3])
                symbol = 1;
            if(e.getSource()==buttons[7])
                symbol = 2;
            if(e.getSource()==buttons[11])
                symbol = 3;
            if(e.getSource()==buttons[15])
                symbol = 4;
                        
            flg = true;
            //text.setText(e.getActionCommand());
            text.setText(" ");
        }
        
        // 判断是否要计算结果
        if(e.getSource()==buttons[14])
        {
            switch(symbol)
            {
                case 1:
                        result = f+l;
                        break;
                case 2:
                        result = f-l;
                        break;
                case 3:
                        result = f*l;
                        break;
                case 4:
                        result = f/l;
                        break;                        
            }
            text.setText(String.valueOf(result));
        }
    }
    
    
    
    public static void main(String[] args)
    {
        new MyCounter();
    }
        
}
搜索更多相关主题的帖子: 计算器 结果 运行 
2008-11-09 11:07
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
收藏
得分:0 
回复 3# 的帖子
Thank you !
    不过还是有点问题:  只能进行一位数运算 。

无师自通
2008-11-09 19:04
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
收藏
得分:0 
有哪位大虾能按照我的思路帮忙改下吗 , 我都想了好几天!!!!!!!!!!!    

无师自通
2008-11-11 10:06
快速回复:“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
数据加载中...
 
   



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

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