有 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);


      }

共 (1) 个答案

  1. # 1 楼答案

    你的问题是,你在读减法符号作为第二个数字的一部分,然后仍然在减法

    例如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]变为numberresult[i]变为result,并且for循环从1到10被移除。(您还必须更改numberresult的声明)