【发布时间】:2017-06-22 17:12:43
【问题描述】:
我有两个关于这个代码的问题。
有人可以解释一下,if 语句到底在做什么。我知道count 必须在每次测试为真时增加,但我不确定这个n % i == 0 在做什么。
我的第二个问题是,如何在控制台上打印 return 语句的答案?
int n = 10;
countFactors(n);
}
public static int countFactors(int n){
int count = 0;
for (int i = 1; i <= n; i++){
if (n % i == 0) //this line
count++;
}
return count;
}
}
【问题讨论】:
-
n % i == 0表示n除以i(n/i的余数为 0) -
if 语句创建不正确,无法编译。括号中的布尔语句正在测试 n 是否可以被 i 整除
-
您的代码在某种程度上是不完整且无法验证的。
-
System.out.println?
-
你明白这行的意思吗?
标签: java if-statement return modulus