Closed. This question needs debugging details。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                    
                
        

我想从一开始就说我还是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);
   }
        }


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

最佳答案

您在源代码结尾处遇到奇怪的情况:

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


如果在if的结尾,这不会因为分号而相乘
这个更好:

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


如果您要这样做,我在这里无法理解您的代码块:

if(condition){
     command1;
     command2;
}


并不是:

{
if(condition)
    command1;
    command2;
}

关于java - 尝试用Java制作功能计算器,我不知道哪里出了问题。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26454094/

10-11 09:21
查看更多