我只需要向我解释一些事情。当我们声明 int 时,我的想法是,只要在开头声明它,在哪里声明并不重要,所以我编写了一小段代码来打印乘法表。
import java.util.Scanner;
public class Learn {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int last = 5;
int i = 1;
while (i <= last){
int j = 1;
while (j <= last) {
System.out.print(i*j);
System.out.print(" " );
j = j + 1;
}
System.out.println();
i = i + 1;
}
}
}
打印出来。
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
但是如果我取 int j = 1;我把它放在上面的 while 循环之外,它只打印出 1 2 3 4 5。为什么会发生这种情况?
最佳答案
如果j = 1
位于循环内部,则每次外循环循环时都会重置。
如果没有,则不会重置,并且 j <= last
的条件在后续循环中将不成立。
关于java - 我不明白为什么要做这个 Action ?我只需要解释一下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359596/