我正在学习如何返回一个值并尝试编写以下代码;
public class JustTryingReturn {
static int a, b;
static Scanner sc = new Scanner(System.in);
static int nganu() {
return a+b;
}
public static void main(String[] args) {
int c = nganu();
System.out.println("Enter number ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println(c);
}
}
但输出始终打印 0 而不是 a+b
。我做错了什么?
谢谢。
最佳答案
你应该打电话
int c = nganu();
分配a
和b
的输入值后。否则,当您计算总和时,默认情况下它们仍将包含 0
。
System.out.println("Enter number ");
a = sc.nextInt();
b = sc.nextInt();
int c = nganu();
System.out.println(c);
关于java - 返回一个值。输出总是打印0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115770/