2017-04-22 66 views
1

我刚开始学习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); 


     } 
+2

很抱歉,但你的代码风格是如此糟糕,我拒绝读它。缩进是一团糟,换行符错误,符号之间的空白符号是错误的,标识符约定被违反,代码注释堆积如山。请阅读Java风格指南并解决此问题...如果您希望其他人花时间阅读您的代码以帮助您。 –

+0

缩小你的代码 –

+0

我建议学习GUI编程之前了解一些IDE调试,然后使用这些技能在这里找出错误,或者创建一个[MCVE],请 –

回答

2

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

E.g. 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当一个标志出现,你可能最终修去的第一个数字的任何情况,所以要小心。


此外,除非有你10次做的每一计算在for回路一个很好的理由,你应该删除这些文件。您的程序同样工作与所有number[i]改为numberresult[i]result(只是测试它),并从1-10 for环取出。 (你也可以改变你的声明numberresult

+0

如何显示减号而不将其应用于显示屏上的号码? –

+0

非常感谢您的帮助。我还有一个问题,我怎么能够同时做多个输出?例如,当我做3 + 2 * 3时,它只会得到最后一个数字作为输出之前的最后一个和第二个数字,并将其相乘,得到6的答案,而不是将3加2 * 3。 –

+0

@ChristopherMoran我认为这个范围足够大,你应该为它提出一个新的问题。它涉及到操作顺序和基本的解析技巧。 – River