4 回答
TA贡献1995条经验 获得超2个赞
你的问题是,在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;
}
}
TA贡献1780条经验 获得超3个赞
您需要在方法中移动代码。其中一个解决方案可能如下所示
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;
}
}
}
但是,根据您的需求,它可能会有所不同。
TA贡献1900条经验 获得超5个赞
您需要在函数中执行代码,例如“包应用程序;
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;
}
}
}`
TA贡献1951条经验 获得超3个赞
与定义类一致:类描述属于它的对象的内容:它描述数据字段(称为实例变量)的聚合,并定义操作(称为方法)。
一个类包含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;
}
}
}
添加回答
举报