Trying to program a scientific calculator in java but can't get the exponent button to work

时间:2017-11-08 21:59:42

标签: java calculator pow exponent

I'm trying to program a scientific calculator with a GUI in java and have been able to do everything so far except for the exponent button (x^y).

Here's what I have for the button click event right now but it doesn't work because I can't figure out how to take in two values while only pressing the button one time.

private void btnExponentActionPerformed(java.awt.event.ActionEvent evt) {                                            

    for (int i = 0; i < 2; i++)
    {
        if(i == 0)
        {
            double x = Double.parseDouble(String.valueOf(tfdDisplay.getText()));
        }
        else if(i == 1)
        {
            double y = Double.parseDouble(String.valueOf(tfdDisplay.getText()));
        }
        tfdDisplay.setText(null);
    }
    double ops = Math.pow(x, y);
    tfdDisplay.setText(String.valueOf(ops));
}                                           

I would like it to take in the value that is currently in the textfield, then have the user click the exponent button, then take in the next value they put in as the actual exponent value, then calculate the answer when they click the "=" button.

I've looked online and found a video that shows how to make a scientific calculator with an exponent button but when I follow his method of coding the button, it does not work correctly. Instead, it just squares what is inside the textfield, rather than letting the user put in their own exponent.

Here's a picture of what the calculator actually looks like for reference.

pic

Thanks in advance!

edit: Here is what I have programmed for the "=" button.

String answer;
        secondnum = Double.parseDouble(tfdDisplay.getText());
        if(operations == "+")
        {
            result = firstnum + secondnum;
            answer = String.format("%.2f", result);
            tfdDisplay.setText(answer);
        }
        else if(operations == "-")
        {
            result = firstnum - secondnum;
            answer = String.format("%.2f", result);
            tfdDisplay.setText(answer);
        }
        else if(operations == "*")
        {
            result = firstnum * secondnum;
            answer = String.format("%.2f", result);
            tfdDisplay.setText(answer);
        }
        else if(operations == "/")
        {
            result = firstnum / secondnum;
            answer = String.format("%.2f", result);
            tfdDisplay.setText(answer);
        }

Should I add this to the "=" button?

else if(operations == "^")
        {
            result = Math.pow(firstnum, secondnum);
            answer = String.format("%.2f", result);
            tfdDisplay.setText(answer);
        }

2 个答案:

答案 0 :(得分:0)

So, if I understand correctly, you hit the buttons digit, then ^, then digit and then = and at this point you have, for example, 2^4 as text in the tfdDisplay element. Then what you should do is split the text and get the two values, like this:

private void btnExponentActionPerformed(java.awt.event.ActionEvent evt) {
    // Notice the double backslash, it's used because split wants a regular
    // expression, and ^ means the beginning of the string in regular
    // expressions, so you have to escape it using a backslash. The other
    // one is needed because you should escape backslashes on strings to use
    // as is
    String parts[] = tfdDisplay.getText().split("\\^");
    double x = Double.parseDouble(parts[0]);
    double y = Double.parseDouble(parts[1]);
    double ops = Math.pow(x, y);
    tfdDisplay.setText(String.valueOf(ops));
}

答案 1 :(得分:0)

所以我想出了如何解决我的问题。

以下是我在按钮点击事件中的内容:

else if(operations == "^")
{
    result = Math.pow(firstnum, secondnum);
    answer = String.format("%.2f", result);
    tfdDisplay.setText(answer);
}

这是我添加到&#34; =&#34;按钮点击事件:

{{1}}

谢谢Obicere!