我创建了一个旨在接受用户输入的代码,然后将其添加5,这就是代码。当我输入任何数字时,它返回0。编辑:我将reCalculate下移了main,没有任何变化
package files;
import java.util.*;
public class CalculatorTest {
static Scanner userFirstNumber = new Scanner(System.in);
static int numberReCalculated;
public static int reCalculate(int a){
int numberReCalculated = a + 5;
return numberReCalculated;
}
public static void main(String[] args){
int bobson;
System.out.print("Enter a number, I will do the rest : ");
bobson = userFirstNumber.nextInt();
reCalculate(bobson);
System.out.println(numberReCalculated);
}
}
最佳答案
您的int numberReCalculated = a + 5;
声明遮盖了字段声明static int numberReCalculated;
。可以将int numberReCalculated = a + 5;
更改为numberReCalculated = a + 5;
,或者将整个代码重写为习惯和有条理的代码:
public class CalculatorTest {
static Scanner userFirstNumber = new Scanner(System.in);
public static int reCalculate(int a){
return a + 5;
}
public static void main(String[] args){
int input;
System.out.print("Enter a number, I will do the rest : ");
input = userFirstNumber.nextInt();
int result = reCalculate(bobson);
System.out.println(result);
}
}
我不知道“ bobson”如何是一个描述性的且具有自我说明性的变量名。
关于java - 我的数学运算Java出错?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25818998/