Java计算器错误 [英] Java Calculator Errors

查看:97
本文介绍了Java计算器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用GUI时,我正在使用JAVA开发一个简单的计算器。我遇到了一些错误,调试器仍然无法解决。.

I am working on a simple calculator in JAVA as i'm trying to experiment with a GUI. I have run into some errors that still aren't resolved with the debugger..

计算器上代表*,-等等的按钮不能正常工作,唯一可以正常工作的是加号按钮,它可以正常工作。

The buttons on the calculator that represent *,- ans so forth aren't working properly, the only one working properly is the plus button which works properly.

除法按钮从不输出任何内容,它输出一个错误,指出不能将其除以零。这是最奇怪的一个,因为如果输入或保存的数字为零,则肯定会影响加号,但不会。

The division button never outputs anything, it outputs an error that says that it cant be divided by zero. This is the strangest one because if the input or the number being saved is zero it would definitely effect the plus answer but it does not.

乘法按钮将零输出为其答案和减号按钮将两个数字加在一起而不是将另一个数字相减。.

The multiply button outputs a zero as its answer ans the minus button adds the two numbers together instead of reducing one from the other..

如果有人发现上述任何问题的原因,请告知我。

If anyone has is seeing the reason of any of the above problems please tell me..

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;

  //Version 1.0

 public class App {
  private JLabel txt;
  private JPanel main;
  private JButton one;
  private JButton three;
  private JButton nine;
  private JButton seven;
  private JButton eight;
  private JButton two;
  private JButton four;
  private JButton six;
  private JButton clear;
  private JButton min;
  private JButton div;
  private JButton times;
  private JButton zero;
  private JButton five;
  private JButton button1;
  private JButton plus;

//Variables are declared
int num1;
int num2;
int ans;

boolean kms = false;

int input;
/* boolean multiplication = false;
boolean division = false;
boolean subtract = false;
boolean addition = false;*/

public App() {

    txt.setText("0");


    times.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("*");
            input = 1;
            kms = true;
        }
    });

    div.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("/");
            kms = true;
            input = 2;
        }
    });

    min.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("-");
            kms = true;
            input = 3;
        }
    });

    plus.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("+");
            kms = true;
            input = 4;
        }
    });

    clear.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ans = 0;
            num1 = 0;
            num2 = 0;
            txt.setText("");
        }
    });

    if(kms == true){

        //Second number being inputted
        num1 = Integer.parseInt(txt.getText());
        txt.setText("");

        one.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText(txt + "1");
                num2 = num2 + 1;

            }
        });
        two.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("2");
                num2 = num2 + 2;
            }
        });
        three.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("3");
                num2 = num2 + 3;
            }
        });
        four.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("4");
                num2 = num2 + 4;
            }
        });
        five.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("5");
                num2 = num2 + 5;
            }
        });
        six.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("6");
                num2 = num2 + 6;
            }
        });
        seven.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("7");
                num2 = num2 + 7;
            }
        });
        eight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("8");
                num2 = num2 + 8;
            }
        });
        nine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("9");
                num2 = num2 + 9;
            }
        });
        zero.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("0");
                num2 = num2 + 0;
            }
        });

        num2 = Integer.parseInt(txt.getText());

    }else if(kms == false){
        //First number being inputted
        one.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("1");
                num1 = num1 + 1;
            }
        });
        two.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("2");
                num1 = num1 + 2;
            }
        });
        three.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("3");
                num1 = num1 + 3;
            }
        });
        four.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("4");
                num1 = num1 + 4;
            }
        });
        five.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("5");
                num1 = num1 + 5;
            }
        });
        six.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("6");
                num1 = num1 + 6;
            }
        });
        seven.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("7");
                num1 = num1 + 7;
            }
        });
        eight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("8");
                num1 = num1 + 8;
            }
        });
        nine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("9");
                num1 = num1 + 9;
            }
        });
        zero.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("0");
                num1 = num1 + 0;
            }
        });

        num2 = Integer.parseInt(txt.getText());

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch(input){
                    case 1:
                        ans = num1 * num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 2:
                        ans = num1 / num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 3:
                        ans = num1 - num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 4:
                        ans = num1 + num2;
                        txt.setText("Answer is " + ans);
                    break;
                }
            }
        });


    }
}




public static void main(String[] args) {

    JFrame frame = new JFrame("Calculator");
    frame.setBackground(Color.white);
    frame.setVisible(true);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new App().main);
    frame.pack();
}

}

推荐答案

根据您在上面的代码中所做的操作,如果您输入的整数只有一位,那么它将非常有效。

According to what you have done in the above code if you enter integers with only one digit it would work perfectly.


Ex:加5,然后 num1 = 5 并按 + 按钮那么 kms true ,然后输入6并 num2 = 6 现在,如果按结果,结果
将为11。

Ex: add 5 then num1=5 and press + button then kms is true, then enter 6 and num2=6 now if you press the result the result will be 11.

但是,如果您尝试在结果前输入另一个数字,则该输入的数字将被添加到 num2 ,例如 num2 = num2 + newAddedDigit 而不是并置为 num2

But if you try to enter another digit before the result, This entered digit will be added to num2 like num2 = num2 + newAddedDigit instead of concatenating to num2

,因此为了获得计算器的真实功能,请尝试连接按下 num1 num2

so in order to get the real functionality of a calculator try to concatenate on number buttons pressed for num1 and num2

这篇关于Java计算器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆