| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 921 人关注过本帖
标题:“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
只看楼主 加入收藏
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
结帖率:100%
收藏
 问题点数:0 回复次数:6 
“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
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
sxl111
Rank: 1
等 级:新手上路
威 望:1
帖 子:55
专家分:0
注 册:2008-5-7
收藏
得分:0 
72461773…………群号…………解答不了……学习了…………
2008-11-09 13:50
learnerboy
Rank: 2
等 级:论坛游民
帖 子:246
专家分:22
注 册:2007-11-11
收藏
得分:0 
我只改了整数运算,小数后面的自己写吧,那个很简单了!
package Test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyCounter extends JFrame implements ActionListener {
    /**
     *
     */
    private static final long serialVersionUID = -2509193275325822061L;

    private JTextField text;

    private JButton[] buttons;

    private int symbol; // symbol表示要进行的运算

    private int f; // f为点击符号前读入的数

    private int l; // l为点击符号后读入的数

    private boolean flg; // flg表示是否已经输入符号  

    private double result; // result为计算结果

    public MyCounter() {
        super("计算器");
        this.setSize(200, 300);
        this.setLocation(400, 300);

        flg = 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 (!flg) {
            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])) {
                text.setText(e.getActionCommand());
                f= Integer.parseInt(text.getText());
                flg = true;
            }
        } else {
            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])) {
                text.setText(e.getActionCommand());
                l = Integer.parseInt(text.getText());
                flg = false;
            }
        }
        /*if(e.getSource()==buttons[13])
         {
         if(point)
         {

         }
         }*/

        // 读入符号
        if (e.getSource() == buttons[3] || e.getSource() == buttons[7]
                || e.getSource() == buttons[11] || e.getSource() == buttons[15]) {
            //设置符号
            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;
        }

        // 判断是否要计算结果
        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(Double.toString(result));
        }
    }

    public static void main(String[] args) {
        new MyCounter();
    }

}
2008-11-09 15:26
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
收藏
得分:0 
回复 3# 的帖子
Thank you !
    不过还是有点问题:  只能进行一位数运算 。

无师自通
2008-11-09 19:04
learnerboy
Rank: 2
等 级:论坛游民
帖 子:246
专家分:22
注 册:2007-11-11
收藏
得分:0 
嘿嘿,任何一样东本都不能做成最好的!那你就好好思考一下怎么才能进行好几位数的运算吧!
2008-11-09 19:46
yinmingzheng13
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-13
收藏
得分:0 
有哪位大虾能按照我的思路帮忙改下吗 , 我都想了好几天!!!!!!!!!!!    

无师自通
2008-11-11 10:06
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
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 double left;               // f为点击符号前读入的数
    private double right;              // l为点击符号后读入的数
   
    private int p;                  // p为当前读入的数字
    private boolean flg;            // flg表示是否已经输入符号
    private boolean press;
    private boolean point;          // 是否已经输入点
    private double result;          // result为计算结果
   
   
    public MyCounter()
    {
        super("计算器");
        this.setSize(200,300);
        this.setLocation(400,300);
        
        flg = false;
        point = false;
        press=false;
        
        left=0;
        right=0;
        
        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");
            flg=false;
        }
        
        // 读入数字并显示在文本框上
        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])
        {
             if(press)
              {
                text.setText("");
                press=false;
              }   
             p = Integer.parseInt(text.getText() + e.getActionCommand());
             text.setText(String.valueOf(p));
             if(flg)
               right=p;
            else
                left=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])
        {
            
           
            //设置符号
           
            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());
            
            press=true;
        }
        
        // 判断是否要计算结果
        if(e.getSource()==buttons[14])
        {   
            
            switch(symbol)
            {
                case 1:
                        left =left+right;
                        break;
                case 2:
                        left = left-right;
                        break;
                case 3:
                        left = left*right;
                        break;
                case 4:
                        left = left/right;
                        break;                        
            }
            flg=true;
            right=0;
            press=true;
            text.setText(String.valueOf(left));
        }
    }
   
   
   
    public static void main(String[] args)
    {
        new MyCounter();
    }
        
}

学习需要安静。。海盗要重新来过。。
2008-11-11 14:15
快速回复:“计算器”能运行,但不能计算结果(诸位大虾帮忙指点下)
数据加载中...
 
   



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

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