我正在解决codechef的INTEST(https://www.codechef.com/problems/INTEST),到目前为止这是我的代码:
//to scan input
import java.util.Scanner;
class Intest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int lines, divisor, counter;
int temporary;
//get input for problem
lines = keyboard.nextInt();
divisor = keyboard.nextInt();
for (int i = 0; i < lines; i++) {
temporary = keyboard.nextInt();
if (temporary % divisor == 0) {
counter++;
}
}
//displays how many ints are divisible by the divisor
System.out.println(counter);
}
}
我应该在本地申报“临时”吗?
答案 0 :(得分:1)
您不会在temporary
循环之外使用for
,因此可以将其for
置于本地,而不会对行为进行任何更改。
答案 1 :(得分:1)
您还需要将变量计数器初始化为0,否则会出现错误:本地变量i可能尚未初始化。
答案 2 :(得分:0)
你不能完全取消temporary
如下吗?
for (int i = 0; i < lines; i++) {
if (keyboard.nextInt() % divisor == 0) {
counter++;
}
}