java - 计算器正在加法而不是减法

标签 java

我刚刚开始学习 Java 中的 GUI,我想知道是否有人可以帮助解决我遇到的问题。我正在尝试制作一个计算器,但问题是每当我减去两个数字并单击等号按钮时,它似乎都是将两个数字相加而不是相减。

  import java.util.Scanner;
  import javax.swing.JFrame;
  import javax.swing.JButton;
  import javax.swing.JLabel;
  import javax.swing.JTextField;
  import java.awt.event.ActionListener;
  import java.awt.Color;
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.GridLayout;
  import java.awt.BorderLayout;
  import java.awt.FlowLayout;
  import javax.swing.JPanel;

  class Colorwindow extends JFrame implements ActionListener {


      private JButton clear, addition, subtract, divide, multiply, zero, one, two, three, four, five, six, seven, eight, nine, ten, equal;
      private JTextField name, name2, name3;
      private String inputing, solution, solution2, solution3, solution4;
      private boolean AddStatement, SubtractStatement, MultiplyStatement, DivideStatement, statement;
      private JButton SButtonList[] = new JButton[6];
      private JButton NButtonList[] = new JButton[10];
      private String SymbolList[] = {
          "+",
          "-",
          "/",
          "*",
          "=",
          "C"
      };
      private String NumberList[] = {
          "0",
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9"
      };
      private String NumberList2[] = {
          "0",
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9"
      };
      private double result[] = new double[10];
      private double number[] = new double[10];
      //AddStatement = false;

      Colorwindow() {

          super();
          setSize(500, 500); //sets size of window
          getContentPane().setBackground(Color.GRAY); //sets backgroundcolor to yellow
          rows 3 column
          JPanel textfont = new JPanel();
          name = new JTextField(30);
          textfont.add(name); //adds textfield
          name.setBackground(Color.CYAN);
          Font bigFont = name.getFont().deriveFont(Font.PLAIN, 70 f);
          name.setFont(bigFont);
          name2 = new JTextField(30);
          //textfont.add(name2);//adds textfield
          add(textfont, BorderLayout.NORTH);
          JPanel rows = new JPanel();
          rows.setLayout(new GridLayout(2, 4));
          for (int i = 0; i < 10; i++) {
              NButtonList[i] = new JButton(NumberList[i]);
              rows.add(NButtonList[i]); //add's the buttons
              NButtonList[i].addActionListener(this);
              add(rows, BorderLayout.CENTER);
          }
          for (int i = 0; i < 6; i++) {
              SButtonList[i] = new JButton(SymbolList[i]);
              rows.add(SButtonList[i]); //add's the buttons
              SButtonList[i].addActionListener(this);
              add(rows, BorderLayout.CENTER);
          }

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes window button when pressing the x(EXIT_ON_CLOSE)
          //add(button);//adds a button to the window//adds componenets to jframe//event
          setTitle("Calculator"); //sets title on top of window
      }

      private static double stringToDouble(String stringObject) {
          return Double.parseDouble(stringObject.trim());
      }
      public void actionPerformed(ActionEvent e) {
          try {

              PassesCorrect(e);




          } catch (NumberFormatException e2) {

              name.setText("Please re-press on a number");




          }

      }

      public void PassesCorrect(ActionEvent e) {

          String ButtonString = e.getActionCommand();
          for (int i = 0; i < 10; i++) {
              if (e.getSource() == NButtonList[i]) {

                  name.setText(name.getText() + NumberList[i]); //appends text

              }
          }
          for (int i = 0; i < 6; i++) {
              if (e.getSource() == SButtonList[i]) {
                  //number = Double.parseDouble(name.getText());
                  name2.setText(SymbolList[i]);
              }
          }

          if (e.getSource() == SButtonList[0]) //checks if it's addition
          {

              for (int i = 0; i < 10; i++) {
                  number[i] = Double.parseDouble(name.getText()); //stores the number that has been entered into an array
              }
              //solution=name.getText();//gets the text and adds it into a string
              name.setText("+"); //sets the number from string and input's it on screen
              AddStatement = true;

          } else if (e.getSource() == SButtonList[1]) //checks if subtraction
          {

              for (int i = 0; i < 10; i++) {
                  number[i] = Double.parseDouble(name.getText());
              }

              //solution=name.getText();//gets the text and adds it into a string
              name.setText("-"); //sets the number from string and input's it on screen
              SubtractStatement = true;

          } else if (e.getSource() == SButtonList[2]) {
              for (int i = 0; i < 10; i++) {
                  number[i] = Double.parseDouble(name.getText());
              }


              //solution=name.getText();//gets the text and adds it into a string
              name.setText("/"); //sets the number from string and input's it on screen
              DivideStatement = true;

          } else if (e.getSource() == SButtonList[3]) {
              for (int i = 0; i < 10; i++) {
                  number[i] = Double.parseDouble(name.getText());
              }


              //solution=name.getText();//gets the text and adds it into a string
              name.setText("*"); //sets the number from string and input's it on screen
              MultiplyStatement = true;

          } else if (e.getSource() == SButtonList[5]) {
              name.setText("");
              SubtractStatement = false;
              AddStatement = false;
              DivideStatement = false;
              MultiplyStatement = false;

          } else if (e.getSource() == SButtonList[4]) //checks if it's equal sign
          {

              for (int i = 0; i < 10; i++) {
                  result[i] = Double.parseDouble(name.getText());
              }


              if (SubtractStatement == true) {
                  for (int i = 0; i < 10; i++) {
                      result[i] = number[i] - result[i];
                      name.setText(Double.toString(result[i]));
                  }




              } else if (AddStatement == true) {
                  for (int i = 0; i < 10; i++) {
                      result[i] = number[i] + result[i];
                      name.setText(Double.toString(result[i]));
                  }

                  //result+=number;


              } else if (MultiplyStatement == true) {

              } else if (DivideStatement == true) {
                  //result=number/result;
                  //name.setText(Double.toString(result));
              }
              SubtractStatement = false;
              AddStatement = false;
              DivideStatement = false;
              MultiplyStatement = false;


          }
      }
  }


  public class GUI2 {

      public static void main(String[] args) {
          // TODO Auto-generated method stub
          Scanner input = new Scanner(System.in);
          Colorwindow W1 = new Colorwindow();
          W1.setVisible(true);


      }

最佳答案

您的问题是您正在读取减法符号作为第二个数字的一​​部分,然后仍然进行减法。

例如2-3 被编码为 2-3,然后你执行 first-second,它变成2-(-3) 本质上是加法。

您可以将其更改为在这两种情况下进行加法,但是一旦您继续进行乘法和除法,就会失败,因为 +4-4 是有效数字,但 *4/4 不是。

相反,这样做

else if(e.getSource()==SButtonList[4])//checks if it's equal sign
              {
                  for(int i =0;i<10;i++)
                  {
                  if(name.getText().length()>0) //make sure this string isn't empty
                    result[i] = Double.parseDouble(name.getText().substring(1));

这将从字符串中获取除第一个字符(即符号)之外的字符。

那是因为我还没有读过你的整个程序,如果在输入此if时出现没有符号的数字,你可能会最终会剪掉第一个数字,所以要小心。

<小时/>

此外,除非有充分的理由让您在 for 循环中进行每项计算 10 次,否则您应该删除这些内容。您的程序的工作方式相同(刚刚测试过)它)将所有 number[i] 更改为 number,将 result[i] 更改为 result,并且 for 1-10 循环已删除。 (您还必须更改 numberresult 的声明)

关于java - 计算器正在加法而不是减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554959/

相关文章:

java - 多个 NULL 添加到 Java 中的列表中

java - 查找 Eclipse 项目中的错误数

java - JPanel removeAll 没有去掉以前的组件

java - 使用 selenium 时如何在 java 中的 Firefox Web 驱动程序上设置页面加载超时?

java - 谓词在 Java 中查找边缘

java - ByteBuddy,在子类化参数化类时是否可以将类分配给泛型

java - java中的文本区域循环

java - 如何对二维数组的元素使用方法?

java - Windows 上的 Spark - 初始化 SparkContext 时出错,Spark URL 无效

java - 如何设计一个对域对象进行大量操作的 API 包装器? (需要指导)