Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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 - Fatal编程技术网

Java 计算器是加法而不是减法

Java 计算器是加法而不是减法,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

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


      }
import java.util.Scanner;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入java.awt.event.ActionListener;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.GridLayout;
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入javax.swing.JPanel;
类Colorwindow扩展JFrame实现ActionListener{
私有JButt清除、加法、减法、除法、乘法、零、一、二、三、四、五、六、七、八、九、十、相等;
私有JTextField名称,name2,name3;
私有字符串输入,解决方案,解决方案2,解决方案3,解决方案4;
私有布尔AddStatement、subtract语句、MultiplyStatement、DivideStatement、statement;
私有JButton SButtonList[]=新JButton[6];
私有JButton NButtonList[]=新JButton[10];
专用字符串符号列表[]={
"+",
"-",
"/",
"*",
"=",
“C”
};
私有字符串NumberList[]={
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
};
私有字符串NumberList2[]={
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
};
私人双精度结果[]=新双精度[10];
私人双倍号码[]=新双倍号码[10];
//AddStatement=false;
彩色窗口(){
超级();
设置大小(500500);//设置窗口大小
getContentPane().setBackground(Color.GRAY);//将backgroundcolor设置为黄色
第3行第3列
JPanel textfont=新的JPanel();
name=新的JTextField(30);
textfont.add(name);//添加textfield
名称.立根背景(颜色:青色);
Font bigFont=name.getFont().deriveFont(Font.PLAIN,70 f);
name.setFont(bigFont);
name2=新的JTextField(30);
//textfont.add(name2);//添加textfield
添加(文本字体,BorderLayout.NORTH);
JPanel行=新的JPanel();
rows.setLayout(新的GridLayout(2,4));
对于(int i=0;i<10;i++){
NButtonList[i]=新的JButton(NumberList[i]);
rows.add(NButtonList[i]);//添加按钮
NButtonList[i].addActionListener(此);
添加(行、边框布局、中心);
}
对于(int i=0;i<6;i++){
SButtonList[i]=新的JButton(符号列表[i]);
rows.add(SButtonList[i]);//添加按钮
SButtonList[i].addActionListener(此);
添加(行、边框布局、中心);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//按x时关闭窗口按钮(EXIT_ON_CLOSE)
//添加(按钮);//将按钮添加到窗口//将组件集添加到jframe//事件
setTitle(“计算器”);//在窗口顶部设置标题
}
私有静态双stringToDouble(String stringObject){
返回Double.parseDouble(stringObject.trim());
}
已执行的公共无效操作(操作事件e){
试一试{
通行证更正(e);
}捕获(NumberFormatException e2){
name.setText(“请重新按一个数字”);
}
}
公共无效通行证更正(行动事件e){
String ButtonString=e.getActionCommand();
对于(int i=0;i<10;i++){
如果(e.getSource()==NButtonList[i]){
name.setText(name.getText()+NumberList[i]);//追加文本
}
}
对于(int i=0;i<6;i++){
如果(e.getSource()==SButtonList[i]){
//number=Double.parseDouble(name.getText());
名称2.setText(符号列表[i]);
}
}
if(e.getSource()==SButtonList[0])//检查它是否为加法
{
对于(int i=0;i<10;i++){
number[i]=Double.parseDouble(name.getText());//存储已输入数组的数字
}
//solution=name.getText();//获取文本并将其添加到字符串中
name.setText(“+”;//根据字符串设置数字并在屏幕上输入
AddStatement=true;
}else if(e.getSource()==SButtonList[1])//检查减法是否正确
{
对于(int i=0;i<10;i++){
number[i]=Double.parseDouble(name.getText());
}
//solution=name.getText();//获取文本并将其添加到字符串中
name.setText(“-”;//根据字符串设置数字并在屏幕上输入
语句=真;
}else if(e.getSource()==SButtonList[2]){
对于(int i=0;i<10;i++){
number[i]=Double.parseDouble(name.getText());
}
//solution=name.getText();//获取文本并将其添加到字符串中
name.setText(“/”;//根据字符串设置数字并在屏幕上输入
DivideStatement=true;
}else if(e.getSource()==SButtonList[3]){
对于(int i=0;i<10;i++){
number[i]=Double.parseDouble(name.getText());
}
//solution=name.getText();//获取文本并将其添加到字符串中
name.setText(“*”;//根据字符串设置数字,并在屏幕上输入
多重
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));