简体   繁体   English

从Java中的计算器获取结果

[英]Getting result from calculator in Java

I have to calculate two inputs from separate JTextfields, pick an operator in a combobox and calculate the result based on the operator chosen. 我必须从单独的JTextfields计算两个输入,在组合框中选择一个运算符,然后根据所选的运算符计算结果。 However, I get 0 as my answer. 但是,我的回答是0。 How can I calculate the result without getting 0? 如何计算结果而不会得到0?

private void jButton1_actionPerformed(ActionEvent e) {

    int x = Integer.parseInt(jTextField1.getText());
    int y = Integer.parseInt(jTextField2.getText());

    String Result = "0";
    jLabel4.setText(Result);
    int total = Integer.parseInt(Result);

    if(Operator.equals("+")) {
        total = x + y;
    }
    else if(Operator.equals("-")) {
        total = x - y;
    }
    else if(Operator.equals("*")) {
        total = x * y;
    }
    else if(Operator.equals("/")) {
        total = x / y;

    }

}

That's because you do not update jLabel4 after calculating the result. 这是因为您没有在计算结果后更新jLabel4。

After the if s you should have add another jLabel4.setText(Integer.toString(result)); if s之后,您应该添加另一个jLabel4.setText(Integer.toString(result));

From this code the jLabel4 is the result label. 从此代码中, jLabel4是结果标签。

What You are doing is first you assign to String Result with "0", and the you set this ("0") as text then you calculate. 您要做的是首先将“ 0”分配给String Result,然后将其设置为“ 0”作为文本,然后进行计算。

What You should do is to calculate first and then set the result. 您应该做的是先计算然后设置结果。

private void jButton1_actionPerformed(ActionEvent e) {

    int x = Integer.parseInt(jTextField1.getText());
    int y = Integer.parseInt(jTextField2.getText());

    int total = 0;

    if(Operator.equals("+")) {
        total = x + y;
    }
    else if(Operator.equals("-")) {
        total = x - y;
    }
    else if(Operator.equals("*")) {
        total = x * y;
    }
    else if(Operator.equals("/")) {
        total = x / y;

    }

    jLabel4.setText(String.valueOf(total));


}

You should separate the method into two parts: one responsible for the calculation of the result and the other for displaying. 您应该将方法分为两部分:一个部分负责计算结果,另一部分负责显示。 In addition to that you probably should use double, otherwise the division will give you unexpected results, ie 0 (eg in case of 1/2). 除此之外,您可能应该使用double,否则除法将为您提供意想不到的结果,即0(例如1/2)。

private void jButton1_actionPerformed(ActionEvent e) {

     int x = Integer.parseInt(jTextField1.getText());
     int y = Integer.parseInt(jTextField2.getText());

     double result = calculateResult(operator, x, y)
     jLabel4.setText(String.valueOf(result));
}

private double calculateResult(String operator, int x, int y) {

     if(operator.equals("+")) {
         total = x + y;
     }
     else if(operator.equals("-")) {
         total = x - y;
     }
     else if(operator.equals("*")) {
         total = x * y;
     }
     else if(operator.equals("/")) {
         total = x / y;

     }
     return total;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM