尝试在 java 中编写科学计算器,但无法使指数按钮工作
Trying to program a scientific calculator in java but can't get the exponent button to work
我正在尝试在 java 中使用 GUI 编写一个科学计算器,到目前为止,除了指数按钮 (x^y) 之外,我已经能够完成所有操作。
这是我现在的按钮点击事件,但它不起作用,因为我无法弄清楚如何在只按下按钮一次的情况下获取两个值。
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));
}
我希望它接受当前文本字段中的值,然后让用户单击指数按钮,然后接受他们输入的下一个值作为实际指数值,然后计算答案他们点击“=”按钮。
我在网上查看并找到了一个视频,该视频展示了如何制作带有指数按钮的科学计算器,但是当我按照他的方法对按钮进行编码时,它无法正常工作。相反,它只是对文本字段内的内容求平方,而不是让用户输入他们自己的指数。
这是计算器的实际外观图片,供参考。
pic
提前致谢!
编辑:
这是我为“=”按钮编写的程序。
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);
}
我应该将它添加到“=”按钮吗?
else if(operations == "^")
{
result = Math.pow(firstnum, secondnum);
answer = String.format("%.2f", result);
tfdDisplay.setText(answer);
}
所以,如果我没理解错的话,你点击了按钮 digit
,然后是 ^
,然后是 digit
,然后是 =
,此时你有,例如,2^4
作为 tfdDisplay
元素中的文本。然后你应该做的是拆分文本并获得两个值,如下所示:
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));
}
所以我想出了解决问题的方法。
这是我在按钮点击事件中放入的内容:
private void btnExponentActionPerformed(java.awt.event.ActionEvent evt) {
firstnum = Double.parseDouble(tfdDisplay.getText());
tfdDisplay.setText(null);
operations = "^";
}
这是我添加到“=”按钮点击事件的内容:
else if(operations == "^")
{
result = Math.pow(firstnum, secondnum);
answer = String.format("%.2f", result);
tfdDisplay.setText(answer);
}
谢谢奥比切雷!
我正在尝试在 java 中使用 GUI 编写一个科学计算器,到目前为止,除了指数按钮 (x^y) 之外,我已经能够完成所有操作。
这是我现在的按钮点击事件,但它不起作用,因为我无法弄清楚如何在只按下按钮一次的情况下获取两个值。
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));
}
我希望它接受当前文本字段中的值,然后让用户单击指数按钮,然后接受他们输入的下一个值作为实际指数值,然后计算答案他们点击“=”按钮。
我在网上查看并找到了一个视频,该视频展示了如何制作带有指数按钮的科学计算器,但是当我按照他的方法对按钮进行编码时,它无法正常工作。相反,它只是对文本字段内的内容求平方,而不是让用户输入他们自己的指数。
这是计算器的实际外观图片,供参考。
pic
提前致谢!
编辑: 这是我为“=”按钮编写的程序。
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);
}
我应该将它添加到“=”按钮吗?
else if(operations == "^")
{
result = Math.pow(firstnum, secondnum);
answer = String.format("%.2f", result);
tfdDisplay.setText(answer);
}
所以,如果我没理解错的话,你点击了按钮 digit
,然后是 ^
,然后是 digit
,然后是 =
,此时你有,例如,2^4
作为 tfdDisplay
元素中的文本。然后你应该做的是拆分文本并获得两个值,如下所示:
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));
}
所以我想出了解决问题的方法。
这是我在按钮点击事件中放入的内容:
private void btnExponentActionPerformed(java.awt.event.ActionEvent evt) {
firstnum = Double.parseDouble(tfdDisplay.getText());
tfdDisplay.setText(null);
operations = "^";
}
这是我添加到“=”按钮点击事件的内容:
else if(operations == "^")
{
result = Math.pow(firstnum, secondnum);
answer = String.format("%.2f", result);
tfdDisplay.setText(answer);
}
谢谢奥比切雷!