在使用if语句时,我不断收到“-令牌“;”上的语法错误,{预期在此令牌之后“错误消息。我不确定为什么,我对java有点陌生。这是一个Java fx项目,但我与控制器没有任何连接。
ive尝试添加else语句并更改变量。香港专业教育学院还试图将声明和修改分开。
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;
}
}
预期结果是干净的程序,没有错误。
答案 0 :(得分:1)
您需要在方法内部移动代码。解决方案之一可能如下
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;
}
}
}
但是,它可能会根据您的需要而有所不同。
答案 1 :(得分:1)
您的问题是,在Java中,每个操作都应在方法内部处理。尝试这样的事情:
public void actions() { //declaring a method
ansAddition = numberOne + numberTwo;
String questionOne = numberOne + " + " + numberTwo + " = ";
ansMultiply = numberOne * numberTwo;
String questionTwo = numberOne + " * " + numberTwo + " = ";
ansDivide = numberOne / numberTwo;
ansSubtract = numberOne - numberTwo;
if (ansAddition > 0) {
answerNumbers = ansAddition;
}
}
答案 2 :(得分:0)
根据定义 班级: 一个类描述了属于它的对象的内容:它描述了数据字段的集合(称为实例变量),并定义了操作(称为方法)。
一个类包含2个实例变量和方法,因此,如果要放置其他东西,则必须借助方法
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
method() {
if (ansAddition > 0) {
answerNumbers = ansAddition;
}
}
}
答案 3 :(得分:0)
您需要在诸如“打包应用程序”之类的函数中执行代码;
public class Mathprocess {
int numberOne = 15;
int numberTwo = 5;
int answerNumbers;
int ansSubtract = 0;
int ansDivide = 0;
int ansMultiply = 0;
int ansAddition = 0;
public static void main(String[] args)
{
//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;
}
}
}`