gpt4 book ai didi

java - javafx 项目中的新类在尝试添加 if 语句时出现错误

转载 作者:行者123 更新时间:2023-12-02 01:34:49 24 4
gpt4 key购买 nike

在使用 if 语句时,我不断收到“- token “;”上的语法错误,{预期在此 token 之后”错误消息。我不知道为什么,我对 java 有点陌生。这是一个 java fx 项目,但我还没有到 Controller 的连接。

我尝试添加 else 语句并更改变量。 ive也尝试将声明和修改分开。

package application;

public class Mathprocess {


int numberOne = 15;
int numberTwo = 5;
int answerNumbers;
int ansSubtract = 0;
int ansDivide = 0;
int ansMultiply = 0;
int ansAddition = 0;




//Question 1
ansAddition = numberOne + numberTwo;
String questionOne = numberOne + " + " + numberTwo +" = ";
//Question 2
ansMultiply = numberOne * numberTwo;
String questionTwo = numberOne + " * " + numberTwo +" = ";

//Question 3
ansDivide = numberOne / numberTwo;




//Question 4
ansSubtract = numberOne - numberTwo;
// error happens here
if (ansAddition > 0) {
answerNumbers = ansAddition;
}

}

预期的结果是一个干净的程序,没有错误。

最佳答案

您需要将代码移至方法内。解决方案之一可能如下

package application;
public class Mathprocess {

public static void main(String[] args){

int numberOne = 15;
int numberTwo = 5;
int answerNumbers;
int ansSubtract = 0;
int ansDivide = 0;
int ansMultiply = 0;
int ansAddition = 0;




//Question 1
ansAddition = numberOne + numberTwo;
String questionOne = numberOne + " + " + numberTwo +" = ";
//Question 2
ansMultiply = numberOne * numberTwo;
String questionTwo = numberOne + " * " + numberTwo +" = ";

//Question 3
ansDivide = numberOne / numberTwo;




//Question 4
ansSubtract = numberOne - numberTwo;
// error happens here
if (ansAddition > 0) {
answerNumbers = ansAddition;
}
}


}

但是,它可能会根据您的需求而有所不同。

关于java - javafx 项目中的新类在尝试添加 if 语句时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55376683/

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