试图在java中制作一个功能计算器,我无法弄清楚我哪里出错了

时间:2014-10-19 18:48:21

标签: java calculator

我想开始说我仍然对Java很新,但我保证这不是一个巨魔帖子。所以我必须为学校制作一个功能四功能计算器。这是非常基本的,但由于某种原因,我无法让它正常工作。我被给了以下作为示例代码,我的老师告诉我们推断其余部分。

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

public class Calculator implements ActionListener 
{

  JFrame f1;
  JTextField display;
  JButton b1,b2,addb,equalsb,clearb;
  JPanel p1;
  String value,operation; 
  double secondnum, total;


   public Calculator()
  {
    value = "";
    total = 0.0;
    secondnum = 0.0;

    f1 = new JFrame("Calculator");
    f1.setSize(400,100);
    Container c1 =  f1.getContentPane();

    display = new JTextField(15);
    b1 = new JButton("1");
    b1.addActionListener(this);                  // adds a Listener for Button b1

    b2 = new JButton("2");
    b2.addActionListener(this); 

    addb = new JButton("+");
    addb.addActionListener(this); 

    equalsb = new JButton("=");
    equalsb.addActionListener(this);

    clearb = new JButton("C");
    clearb.addActionListener(this);

    p1 = new JPanel();

    p1.add(display);
    p1.add(b1);
    p1.add(b2);
    p1.add(addb);
    p1.add(equalsb);
    p1.add(clearb);
    c1.add(p1);

    f1.show();

   }



    public void actionPerformed(ActionEvent event)        
      {                                                    
       if (event.getSource()== b1)      
          value = value + "1";

      if (event.getSource() == b2)     
           value = value + "2";

      if (event.getSource() == clearb)
         {
           total = 0.0; 
           secondnum = 0.0;
           value = "";
         }


      if (event.getSource() == addb)
        {
           total = Double.parseDouble(value);
           operation = "add";
           value = "";
        }

      if (event.getSource() == equalsb)
        {
          secondnum = Double.parseDouble(value);   

          if(operation.equals("add"))
              total = total + secondnum;

          value = ""+ total;            //value becomes what the total is to be diplays  
        }   


   display.setText(value);
    }
  }

这是有效的,所以我创建了以下内容。我很确定我在功能键上出错了,但我不清楚我搞砸了哪里。

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

public class Calculator implements ActionListener
{
JFrame f;
Container c; 
JPanel p; 
JTextField display; 
JButton b1, b2, b3, bdiv, b5, b6, b7, bmult, b9, b10, b11, badd, b13, bdecimal, bequals, bsub,        bclear;
String value, operation;
double secondnum, total;
public Calculator()
{
  value = "";
  total = 0.0;
  secondnum = 0.0;
  f = new JFrame("Calculator");
  f.setSize(333,225);
  c = f.getContentPane();
  p = new JPanel();
  display = new JTextField(20);
  b1 = new JButton("7");
  b1.addActionListener(this);
  b2 = new JButton("8");
  b2.addActionListener(this);
  b3 = new JButton("9");
  b3.addActionListener(this);
  bdiv = new JButton("/");
  bdiv.addActionListener(this);
  b5 = new JButton("4");
  b5.addActionListener(this);
  b6 = new JButton("5");
  b6.addActionListener(this);
  b7 = new JButton("6");
  b7.addActionListener(this);
  bmult = new JButton("x");
  bmult.addActionListener(this);
  b9 = new JButton("1");
  b9.addActionListener(this);
  b10 = new JButton("2");
  b10.addActionListener(this);
  b11 = new JButton("3");
  b11.addActionListener(this);
  badd = new JButton("+");
  badd.addActionListener(this);
  b13 = new JButton("0");
  b13.addActionListener(this);
  bdecimal = new JButton(".");
  bdecimal.addActionListener(this);
  bequals = new JButton("=");
  bequals.addActionListener(this);
  bsub = new JButton("-");
  bsub.addActionListener(this);
  bclear = new JButton("C");
  bclear.addActionListener(this);
  p.setBackground(Color.green);
  p.add(display);
  p.add(b1);
  p.add(b2);
  p.add(b3);
  p.add(bdiv);
  p.add(b5);
  p.add(b6);
  p.add(b7);
  p.add(bmult);
  p.add(b9);
  p.add(b10);
  p.add(b11);
  p.add(badd);
  p.add(b13);
  p.add(bdecimal);
  p.add(bequals);
  p.add(bsub);
  p.add(bclear);
  c.add(p);
  f.show();
 }
 public void actionPerformed (ActionEvent event) 
    {

        if(event.getSource() == b1)
          {
              value = value + "7";
            }       


        if(event.getSource() == b2)
          {
             value = value + "8";
            }


        if(event.getSource() == b3)
          {
             value = value + "9";
            }  




        if(event.getSource() == b5)
          {
              value = value + "4";
            }


        if(event.getSource() == b6)
          {
              value = value + "5";
            }


        if(event.getSource() == b7)
          {
              value = value + "6";
            }





        if(event.getSource() == b9)
          {
             value = value + "1";
            }


        if(event.getSource() == b10)
          {
              value = value + "2";
            }


        if(event.getSource() == b11)
          {
              value = value + "3";
            }





        if(event.getSource() == b13)
          {
              value = value + "0";
            }


        if(event.getSource() == bdecimal)
          {
             value = value + ".";
            }  

         if(event.getSource() == badd)
          {
             total = Double.parseDouble(value);
              operation = "add";
              value = "";
            }

        if(event.getSource() == bmult)
          {
              total = Double.parseDouble(value);
              operation = "multiply";
              value = "";
            }

       if(event.getSource() == bsub)
          {
               total = Double.parseDouble(value);
              operation = "subtract";
              value = "";
            }

        if(event.getSource() == bdiv)
          {
              total = Double.parseDouble(value);
              operation = "divide";
              value = "";
            }     

       if(event.getSource() == bclear)
          {
              total = 0.0;
              secondnum = 0.0;
              value = "";
            }

       if(event.getSource() == bequals)
       {
           secondnum = Double.parseDouble(value);
         {  
           if(operation.equals("add));
           total = total + secondnum;
        }
         {  
           if(operation.equals("subtract"));
           total = total - secondnum;
        }
           if(operation.equals("divide"));
           total = total / secondnum;
          {  
           if(operation.equals("multiply"));
           total = total * secondnum;
        }
        value = "" + tottal
        }
        display.setText(value);
   }
        }    

非常感谢任何帮助,我意识到这可能只是一个愚蠢的错误。

1 个答案:

答案 0 :(得分:2)

您在源代码的末尾有一些奇怪的条件:

if(operation.equals("multiply"));
       total = total * secondnum;
由于分号结束,因此,这不会成倍增加 这样更好:

if(operation.equals("multiply"))
       total = total * secondnum;

如果你想这样做,我无法理解你的代码块:

if(condition){
     command1;
     command2;
}

而不是:

{
if(condition)
    command1;
    command2;
}