gpt4 book ai didi

java - 我不明白为什么要做这个 Action ?我只需要解释一下

转载 作者:行者123 更新时间:2023-12-02 04:49:31 25 4
gpt4 key购买 nike

我只需要向我解释一些事情。当我们声明 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 52 4 6 8 103 6 9 12 154 8 12 16 205 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com