gpt4 book ai didi

java - JavaFX 计算器中的方程式问题

转载 作者:行者123 更新时间:2023-11-29 08:21:57 25 4
gpt4 key购买 nike

我的 JavaFX/Java 计算器有问题。当我输入方程式时,它(计算器)工作正常,答案也是正确的。但是当我在另一个操作中按下/键入另一个数字时,结果/总和/等。除非我按下/键入的运算符(operator)与以前相同,否则将不正确。这是一个例子:

作品:2 + 6 = 8 + 2 = 10

不起作用:2 + 6 = 8 - 2 = 10

作品:6 x 2 = 12 x 2 = 26

不起作用:6 x 2 = 12 ÷ 2 = 26

我想知道是否有任何方法可以解决此问题。

这是我的 FXML Controller 类:

package calculator;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import java.lang.Math;

public class Calculator implements Initializable {

double data = 0d;
int operation = -1;
private boolean start = false;

@FXML
private Label display;

@FXML
private Button two;

@FXML
private Button five;

@FXML
private Button four;

@FXML
private Button three;

@FXML
private Button one;

@FXML
private Button six;

@FXML
private Button seven;

@FXML
private Button multi;

@FXML
private Button add;

@FXML
private Button divide;

@FXML
private Button minus;

@FXML
private Button equals;

@FXML
private Button clear;

@FXML
private Button zero;

@FXML
private Button nine;

@FXML
private Button eight;

@FXML
void handleButtonAction(ActionEvent event) {

if(start)
{
display.setText("");
start = false;
}

if(event.getSource() == one)
{
display.setText(display.getText() + "1");
}
else if(event.getSource() == two)
{
display.setText(display.getText() + "2");
}
else if(event.getSource() == three)
{
display.setText(display.getText() + "3");
}
else if(event.getSource() == four)
{
display.setText(display.getText() + "4");
}
else if(event.getSource() == five)
{
display.setText(display.getText() + "5");
}
else if(event.getSource() == six)
{
display.setText(display.getText() + "6");
}
else if(event.getSource() == seven)
{
display.setText(display.getText() + "7");
}
else if(event.getSource() == eight)
{
display.setText(display.getText() + "8");
}
else if(event.getSource() == nine)
{
display.setText(display.getText() + "9");
}
else if(event.getSource() == zero)
{
display.setText(display.getText() + "0");
}
else if(event.getSource() == clear)
{
display.setText("");
}
else if(event.getSource() == add)
{
data = Float.parseFloat(display.getText());
operation = 1;
display.setText("");
}
else if(event.getSource() == minus)
{
data = Float.parseFloat(display.getText());
operation = 2;
display.setText("");
}
else if(event.getSource() == multi)
{
data = Float.parseFloat(display.getText());
operation = 3;
display.setText("");
}
else if(event.getSource() == divide)
{
data = Float.parseFloat(display.getText());
operation = 4;
display.setText("");
}
else if(event.getSource() == equals)
{
Float secondOperand = Float.parseFloat(display.getText());
switch(operation)
{
case 1: //Addition
Double ans = data + secondOperand;
display.setText(String.valueOf(ans));
//data = ans;
break;
case 2: //Subtraction
ans = data - secondOperand;
display.setText(String.valueOf(ans));
//data = ans;
break;
case 3: //Multiplication
ans = data * secondOperand;
display.setText(String.valueOf(ans));
//data = ans;
break;
case 4: //Division
ans = 0d;
try{
ans = data / secondOperand;
}catch(Exception ex){display.setText("Error");}
display.setText(String.valueOf(ans));
//data = ans;
break;
}
if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus)
{
start = true;
}
}

}

@FXML
private void send2sceneconver(ActionEvent event) throws Exception{
Parent rootBMI = FXMLLoader.load(getClass().getResource("ConversionCal.fxml"));

Scene scene2 = new Scene(rootBMI);
Stage calS = (Stage) ((Node) event.getSource()).getScene().getWindow();

calS.setScene(scene2);
calS.show();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

如有任何帮助,我们将不胜感激。

最佳答案

您为按下 equals 按钮执行以下操作

else if(event.getSource() == equals)
{
...
if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus)
{
start = true;
}
}

因为 sourceequals 而不是其他任何一个,所以您总是将 start 设置为 true按下一个按钮会将 display 的内容替换为空字符串:

if(start)
{
display.setText("");
start = false;
}

这会导致 NumberFormatException 被下一次调用抛出

data = Float.parseFloat(display.getText());

由于对operation 字段的赋值发生在该调用之后,因此在按下equals 后立即按下运算符按钮不会导致字段值和旧值的更新运算符始终保留。

您应该做的是引入一个字段,告诉您第一个操作数是否可用或是否必须解析。

private final Set<Button> numbers = new HashSet<>();
private final Map<Button, Integer> operators = new HashMap<>();

@Override
public void initialize(URL url, ResourceBundle rb) {
numbers.addAll(Arrays.asList(zero, one, two, three, four, five, six, seven, eight, nine));
operators.put(add, 1);
operators.put(minus, 2);
operators.put(multi, 3);
operators.put(divide, 4);
}

private double data;
private boolean dataAvailable = false;

@FXML
private void handleButtonAction(ActionEvent event) {
Button source = (Button) event.getSource();

if (source == clear) {
dataAvailable = false;
display.setText("");
operation = 0;
} else if (source == equals) {
double secondOperand;
try {
secondOperand = Double.parseDouble(display.getText());
} catch (NumberFormatException ex) {
return; // only continue, if parsing is successful
}
double result;
switch (operation) {
case 1: //Addition
result = data + secondOperand;
break;
case 2: //Subtraction
result = data - secondOperand;
break;
case 3: //Multiplication
result = data * secondOperand;
break;
case 4: //Division
double res = data / secondOperand;
if (Double.isFinite(res)) {
result = res;
} else {
// TODO: division by zero
}
break;
default:
return; // ignore press, if operand is not set yet
}
display.setText(Double.toString(result));
operation = 0;
data = result;
dataAvailable = true;
} else if (numbers.contains(source)) {
if (!dataAvailable) { // just ignore input, if = gave us the first operand
display.setText(display.getText() + source.getText());
}
} else {
Integer op = operators.get(source);
if (op != null) {
if (!dataAvailable) {
try {
data = Double.parseDouble(display.getText());
} catch (NumberFormatException ex) {
return; // do nothing on invalid input
}
} else {
dataAvailable = false;
}
display.setText("");
operation = op;
}
}
}

关于java - JavaFX 计算器中的方程式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com