Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试用java制作一个函数计算器,我可以';我不知道我在哪里';我做错了_Java_Calculator - Fatal编程技术网

尝试用java制作一个函数计算器,我可以';我不知道我在哪里';我做错了

尝试用java制作一个函数计算器,我可以';我不知道我在哪里';我做错了,java,calculator,Java,Calculator,我想首先说我对java还是一个新手,但我保证这不是一篇巨魔的帖子。所以我必须为学校做一个四功能计算器。这真的很基本,但由于某种原因,我不能让它正常工作。我得到了下面的示例代码,我的老师告诉我们推断其余的 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator implements ActionListener { JFrame f1; JTextField d

我想首先说我对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;
}

它到底是如何工作不正常的?你可以从使用更好的变量名开始(即使用
b1
be-1按钮等),然后改进你的问题,向我们提供关于你的问题和困惑的更多细节。是的,这些变量真的很愚蠢,我还没有来得及解决它。它不工作,因为我可以尝试做3*3,它仍然会显示3.0。我只是看不出哪里出了问题。它对所有函数都执行相同的操作。代码中存在多个语法错误。也是一条类似if(operation.equals(“multiply”))的线;如果(operation.equals(“multiply”){//代码在这里}这里还有另一个错误:value=”“+total。这里的total是用一个双t拼写的,但是没有变量tottal。好吧,它会相乘,但每次都是,不仅仅是,当按下
乘法
按钮时。当我删除四个分号并修复代码中接下来的两个错误时,它可以正常工作@TomI只是指句子
不会乘法,因为if
末尾有分号。不完全正确。你是对的,我的错,我写它是为了简化,为了更好地理解问题,不完全正确,我的意思是将执行command
total=total*secondnum
,但不取决于if语句,因为if只执行下一个命令或命令块,如果有分号,分号命令(无命令)将被执行,所有操作都将被执行,因此它将被添加,然后被减,然后被除,然后被乘,所以你将得到相同的数字…,对吗@Tom?嗯,我不确定,如果
将被解释为命令或
if
块/语句的终止。但这并不重要:D。