我的JavaFX / Java计算器有问题。当我输入方程式时,它(计算器)工作正常,答案也正确。但是,当我按下/键入另一个操作,然后再键入另一个数字时,结果/ sum / etc。除非我按下/键入的操作员与以前的操作员相同,否则不会正确。这是一个示例:
有效: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
}
}
任何帮助将不胜感激。
答案 0 :(得分:2)
所以我不确定为什么会出现问题,但是问题出在这个问题上
if(start)
{
display.setText("");
start = false;
}
我已经用您的作品对它进行了测试,而不是方程,一切似乎都还不错。中断它的原因是正在执行下一个操作时重置了显示,因此在观看时看起来不错,但是在尝试执行操作时数据被清除了。因此它没有最新数据可供提取。我把它注释掉了,其余的似乎都在这里工作,这是我的课程供验证
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.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
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
}
}
另外请注意,如果您不使用implements Initializable
,可以将其删除
答案 1 :(得分:2)
您需要执行以下操作才能按下equals
按钮
else if(event.getSource() == equals)
{
...
if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus)
{
start = true;
}
}
由于source
为equals
,并且都不是其他任何一个,因此您始终将start
设置为true
,这会在按下下一个按钮时替换{{1 }},其中包含空字符串:
display
这将导致下一次调用时抛出if(start)
{
display.setText("");
start = false;
}
NumberFormatException
由于对data = Float.parseFloat(display.getText());
字段的分配是在此调用之后发生的,因此仅在按下operation
之后再按一个运算符按钮不会导致该字段的值更新,并且始终保留旧的运算符。 / p>
您应该做的是引入一个字段,告诉您第一个操作数是否可用或必须进行解析。
equals
答案 2 :(得分:0)
我还想出了另一种方法,像这样在[data =]之前声明[operation =]:
else if(event.getSource() == divide)
{
operation = -1;
operation = 4;
data = Float.parseFloat(display.getText());
display.setText("");
dot.setDisable(false);
}