对于陈述结果无法理解

时间:2014-10-09 17:59:29

标签: java

我的AP计算机科学课程的老师给了我们这段代码

final int LIMIT = 5;
int i, count;
for (count=1; count<=LIMIT; count++) 
{
    for (i=0; i<count; i++) 
    {
        System.out.print(count);
    }
}
System.out.println();

执行此代码时,输​​出如下

1
22
333
4444
55555

我对for语句没有很好的理解,我无法理解为什么代码会打印1次,2次,3次,3次等等。有人可以试着向我解释一下吗?

6 个答案:

答案 0 :(得分:0)

第一个for循环表示它将循环内容LIMIT次(即5),在每个循环结束时将count递增1。第二个内部for循环表示打印数字count的次数。

使用缩进代码可能更容易理解:

final int LIMIT = 5;
int i, count;
for (count=1; count<=LIMIT; count++) {
    for (i=0; i<count; i++) {
        System.out.print(count);
    }
    System.out.println();
}

答案 1 :(得分:0)

第一个for循环从1开始并以极限5结束。内循环从0开始并转到当前计数。所以对于第一次迭代,内部循环将从0变为1.这将打印计数1次,其中计数为1.第二次通过外部循环,计数器将为2,因此内部循环将从0循环到2。将打印两次。第三次计数将是3.内循环将从0循环到3并打印3次3次。等等......这是嵌套循环。

答案 2 :(得分:0)

第一个循环将写入5行作为LIMIT = 5

对于每一行,第二个循环将写入第一个循环的当前索引的n倍。

n等于第一个循环的当前索引。所以行#1,一次1,行#2,两次2,....

答案 3 :(得分:0)

您有两个for循环,外循环以值{1开始count,并将迭代直到count的值大于5.内部for循环将以值0开始,并将迭代直到i的值大于或等于count。所以,

count <- 1
i <- 0
while (i < count) { print(count); i++; } // <-- 0 to 1 is a range of 1
newline();

然后

count <- 2
i <- 0
while (i < count) { print(count); i++; }  // <-- 0 to 2 is a range of 2
newline();

等等。

答案 4 :(得分:0)

这是一个很棒的问题,涵盖了for循环的基础知识。

考虑循环:基础知识

以这种方式考虑上面编写的代码:

for循环执行(“执行”)每次指定时在其正文中包含的内容 - 或者,以另一种方式考虑它,直到您指定的条件为false 。作为编码员,你需要告诉程序:[1]在什么时候开始; [2]多少次这样做(更好地考虑“直到做到这一点”); [3]以及代码应该“计数”多少(我们称之为“增量”)。

使用伪代码进行演示:

for ([1]; [2]; [3])

for (where to begin; how many times; how to count) {
    // DO SOMETHING
}

请记住,当您“计算”时,您正在增加 - 在第一个for循环的情况下 - count变量how to count变量count + how to count)。您在上面的代码中有一个嵌套 for循环 - 另一个循环中的循环(如下所述)。引用我上面写的伪代码,您的第一个for循环执行以下操作:

for (start counting at 1; iterate once until count is greater than 5; increment count by 1)

您的问题

现在,这件事正在抛弃你。你需要注意两个变量,因为你有两个for循环:count,在第一个for循环中(由第二个使用);第二个i。每次第一个for循环迭代时,第一个for循环必须执行第二个for循环(查看代码)。再次,注意两个变量 - 但更重要的是,两个for循环'条件中的变量之间的关系。第二个for循环取决于第一个。以这种方式思考:

count递增时(此处增加1),第二个for循环必须多次执行自己的操作:

for (i starting at 0; i not equal to count; increment by 1)

所以,在第一次迭代中你会看到类似这样的东西:

for (count = 1; count will always iterate once until condition false; increment){ }
for (i = 0; i < count (1); increment i){ }

结果将是:

1

然后第一个循环递增(count变为2),你得到:

for (i = 0; i < (2); increment i)

因为第二个循环将迭代,直到i不再小于2不包括2 ),您的结果将是:

22

此关系将持续到您获得最终结果为止:

55555

最终评论

您的老师希望通过上面提供的代码教您一些不同的东西,即两个:嵌套for循环;以及如何初始化迭代器(icount)以及条件语句<><=>=等)影响循环的行为。

答案 5 :(得分:0)

最好通过一个步骤来解释这一点......

final int LIMIT = 5;
int i, count;
for (count=1; count<=LIMIT; count++) 
{
    for (i=0; i<count; i++) 
    {
        System.out.print(count);
    }
   System.out.println(); //from the results you are getting, this statement should be here
}

用可读的术语表示:

A. we have a number LIMIT that is 5
B. we have a number i and count
C. set the number count to 1
D. if the number count is less than LIMIT, do the following:
      1. set the number i to 0
      2. if the number i is less than the number count, do the following:
             a. print out the number count
             b. add 1 to the number i
             c. go back to step 2
      3. print out an empty line
      4. add 1 to the number count   
      5. go back to step D

所以,如果我们通过循环,这就是我们的变量:

count is set to 1
count is less than 5
i is set to 0
i is less than count? yes, i is 0 and count is 1
print out the value of count (which is 1)
add 1 to i
i is 1
is i less than count? no, i is 1 and count is 1
print out a new line
add 1 to count
count is 2
count is less than 5
i is set to 0
i is less than count? yes, i is 0 and count is 2
print out the value of count (which is 2)
add 1 to i
is i less than count? yes, i is 1 and count is 2
print out the value of count (which is 2)
add 1 to i
is i less than count? no, i is 2 and count is 2
print out a new line
add 1 to count
count is 3
etc.

这一直持续到count大于5,此时它就存在了。从模式中可以看出,变量count确定打印哪个数字,变量i确定打印的次数。