计算器正在添加数字而不是减去数字

时间:2017-04-22 03:37:53

标签: java

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

  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 个答案:

答案 0 :(得分:2)

你的问题是你在第二个数字中读取减法符号,然后仍然减去。

E.g。 2-3被编码为2-3,然后你会first-second转变为2-(-3),这实际上是加法。

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

相反,做

/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循环中进行10次计算,你应该删除它们。你的程序工作方式相同(只是测试过)所有for更改为number[i]number更改为result[i],并删除了1-10的result次循环。 (您还必须更改fornumber

的声明