Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java for循环很难理解_Java_Loops - Fatal编程技术网

Java for循环很难理解

Java for循环很难理解,java,loops,Java,Loops,有人能给我解释一下为什么下面的代码块会产生如此不同的输出吗 public class hello { public static void main(String args[]) { int a,b,c; for (a = 0; a < 5; a++) { for (b = 4; b >= a; b--) { System.out.pr

有人能给我解释一下为什么下面的代码块会产生如此不同的输出吗

public class hello 
{

    public static void main(String args[])
    {
        int a,b,c;
        for (a  = 0; a < 5; a++)
        {
            for (b = 4; b >= a; b--)
            {
                System.out.print(" ");
            }
            for (c = 0; c <= a - b; c++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
公共类你好
{
公共静态void main(字符串参数[])
{
INTA、b、c;
对于(a=0;a<5;a++)
{
对于(b=4;b>=a;b--)
{
系统输出打印(“”);
}
对于(c=0;c=0;b--)
{
系统输出打印(“”);
}

对于(c=0;c=a相当于b>=0,因为b的值将在每个循环中减去1?

变量
a
将从0变为4,因此对于每个迭代,您将有:

for (b = 4; b >= 0; b--)
    { [...]

for (b = 4; b >= 1; b--)
    { [...]

for (b = 4; b >= 2; b--)
    { [...]

变量
a
将从0变为4,因此对于每个迭代,您将有:

for (b = 4; b >= 0; b--)
    { [...]

for (b = 4; b >= 1; b--)
    { [...]

for (b = 4; b >= 2; b--)
    { [...]

不,输出不能相同,因为每次执行时

for (a = 0; a < 5; a++)
但是
a
的值随着每次迭代而变化:
a
最初是0,然后是1、2、3,最后是4

因此,在第一个场景中打印的空格数将随着
a
的每次迭代而减少

对于a=0,我们有: b=4, b=3, b=2, b=1, b=0(
for循环
停止,因为b=-1不是>=a=0)

对于a=1,我们有: b=4, b=3, b=2, b=1(
for循环
停止,因为b=0不是>=a=1)

对于a=2,我们有: b=4, b=3, b=2(
for loop
停止,因为b=1不是>=a=2)

对于a=3,我们有: b=4, b=3(
for loop
停止,因为b=2不是>=a=3)

对于a=4,我们有: b=4(
for loop
停止,因为b=3不是>=a=4)


不,输出不能相同,因为每次执行时

for (a = 0; a < 5; a++)
但是
a
的值随着每次迭代而变化:
a
最初是0,然后是1、2、3,最后是4

因此,在第一个场景中打印的空格数将随着
a
的每次迭代而减少

对于a=0,我们有: b=4, b=3, b=2, b=1, b=0(
for循环
停止,因为b=-1不是>=a=0)

对于a=1,我们有: b=4, b=3, b=2, b=1(
for循环
停止,因为b=0不是>=a=1)

对于a=2,我们有: b=4, b=3, b=2(
for loop
停止,因为b=1不是>=a=2)

对于a=3,我们有: b=4, b=3(
for loop
停止,因为b=2不是>=a=3)

对于a=4,我们有: b=4(
for loop
停止,因为b=3不是>=a=4)


请阅读代码中的注释,您将看到差异

    int a, b, c;
    for (a = 0; a < 5; a++)
    {
        for (b = 4; b >= a; b--) // Print every time b-a + 1 underscores... since you start every time with b=4 you have for each a one space fewer
        {
            System.out.print(" ");
        }

        for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time a-1)... 
                                     // (first a=0 -b=-1)+1=2 and any time it will prit 2 stars scince
        {
            System.out.print("*");
        }
        System.out.println();
    }

    for (a = 0; a < 5; a++)
    {
        for (b = 4; b >= 0; b--) // Print every time b-a + 1=4 underscores
        {
            System.out.print(" ");
        }

        for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time -1)... 
                                     // first time ( a=0 -b=-1)+1 =2 , second time (a=1 - b=-1)+1=3
        {
            System.out.print("*");
        }

        System.out.println();
    }
inta、b、c;
对于(a=0;a<5;a++)
{
for(b=4;b>=a;b--)//每次b-a+1下划线打印一次……因为每次从b=4开始,每个a的空间都少了一个
{
系统输出打印(“”);
}
for(c=0;c=0;b--)//每次b-a+1=4下划线时打印
{
系统输出打印(“”);
}

对于(c=0;c请阅读代码中的注释,您将看到差异

    int a, b, c;
    for (a = 0; a < 5; a++)
    {
        for (b = 4; b >= a; b--) // Print every time b-a + 1 underscores... since you start every time with b=4 you have for each a one space fewer
        {
            System.out.print(" ");
        }

        for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time a-1)... 
                                     // (first a=0 -b=-1)+1=2 and any time it will prit 2 stars scince
        {
            System.out.print("*");
        }
        System.out.println();
    }

    for (a = 0; a < 5; a++)
    {
        for (b = 4; b >= 0; b--) // Print every time b-a + 1=4 underscores
        {
            System.out.print(" ");
        }

        for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time -1)... 
                                     // first time ( a=0 -b=-1)+1 =2 , second time (a=1 - b=-1)+1=3
        {
            System.out.print("*");
        }

        System.out.println();
    }
inta、b、c;
对于(a=0;a<5;a++)
{
for(b=4;b>=a;b--)//每次b-a+1下划线打印一次……因为每次从b=4开始,每个a的空间都少了一个
{
系统输出打印(“”);
}
for(c=0;c=0;b--)//每次b-a+1=4下划线时打印
{
系统输出打印(“”);
}

对于(c=0;c)这些是嵌套循环,对于从0到4(小于5)的每个值a,您将循环从4到a的每个值(第一次是0,然后是1,然后是2,以此类推)当
a==1
a==2
时,它与
a==0
不一样,我认为b对于每个循环都有新的值,因为b存在,这意味着循环每次都会变小…所以在这种情况下它什么都不做?在第一个程序中,b依赖于a,所以空格在每行中按降序打印。但是在第二个程序中,b在整个循环中是相同的,但c依赖于a,所以它以递增的顺序打印“星”。这些是嵌套循环,对于从0到4(小于5)的每个值a,您在从4到a的每个值上循环(第一次是0,然后是1,然后是2,以此类推)当
a==1
a==2
时,它与
a==0
不一样,我认为b对于每个循环都有新的值,因为b存在,这意味着循环每次都会变小…所以在这种情况下它什么都不做?在第一个程序中,b依赖于a,所以空格在每行中按降序打印。但是在第二个程序中,b在整个循环中都是相同的,但c依赖于a,所以它以递增的顺序打印“星”。所以b--在这种情况下什么都不做?对不起,我是java新手:(我以为b在每个循环中都有新的值,因为b在那里,这意味着循环每一个循环都会更小。)time@ThĕnhĐạ你能做的最好的事情就是在纸上用每个变量值写下每次迭代。你也必须了解for循环的每个部分。第一个是init,第二个是迭代前的条件,最后一个是迭代后执行的语句。所以b--在这种情况下什么都不做?对不起,我是java新手:(我原以为每个循环都有一个新的值,因为b存在,这意味着循环每一个循环都会更小。)time@ThĕnhĐạ你能做的最好的事情就是在纸上写下每次迭代的每个变量值。你也必须了解for循环的每个部分。第一个是init,第二个是迭代前的条件,最后一个是迭代后执行的语句。我认为b会为每个循环都有新的值,因为b存在,这意味着循环每次都会变小,这与第一个代码块的大小相同…所以在这里