我可以将for循环数字模式转换为while循环和do while循环吗?

huangapple go评论82阅读模式
英文:

Can I convert for loop number pattern to while loop and do while loop?

问题

以下是您提供的代码的翻译结果:

使用for循环打印数字"1"图案的代码如下:

for (int i = 0; i <= 7; i++) {
    for (int j = 0; j <= 4; j++) {
        if ((i == 0 && j > 1) || (i == 1 && j > 0) || (i == 2 && j >= 0) || (i == 3 && j > 1) || (i > 3 && j > 1))
            System.out.print("1");
        else
            System.out.print(" ");
    }
    System.out.println();
}

使用while循环打印数字"1"图案的代码如下:

int i = 0, j = 0;
while (i <= 7) {
    while (j <= 4) {
        if ((i == 0 && j > 1) || (i == 1 && j > 0) || (i == 2 && j >= 0) || (i == 3 && j > 1) || (i > 3 && j > 1))
            System.out.print("1");
        else
            System.out.print(" ");
            
        j++;
    }
    System.out.println();
    i++;
}

我可以将for循环数字模式转换为while循环和do while循环吗?

英文:

I'm learning java looping, and managed to print a number one pattern using the for loop.
I tried printing the number one pattern using the while and do while loop, but had difficulty. This is my code:

for (int i = 0; i &lt;= 7; i++) {
    for (int j = 0; j &lt;= 4; j++) {
        if ((i == 0 &amp;&amp; j &gt; 1) || (i == 1 &amp;&amp; j &gt; 0) || (i == 2 &amp;&amp; j &gt;= 0) || (i == 3 &amp;&amp; j &gt; 1) || (i &gt; 3 &amp;&amp; j &gt; 1))
            System.out.print(&quot;1&quot;);
        else
            System.out.print(&quot; &quot;);
    }

    System.out.println();
}

This is my while loop code:

int i = 0, j = 0;
        while (i &lt;= 7) {
            while (j &lt;= 4) {
                if((i == 0 &amp;&amp; j &gt; 1) || (i == 1 &amp;&amp; j &gt; 0) || (i == 2 &amp;&amp; j &gt;= 0) || (i == 3 &amp;&amp; j &gt; 1) || (i &gt; 3 &amp;&amp; j &gt; 1))
                    System.out.print(&quot;1&quot;);
                else
                    System.out.print(&quot; &quot;);
                    
                j++;
            }
            
            System.out.println();
            i++;
        }

我可以将for循环数字模式转换为while循环和do while循环吗?

答案1

得分: 1

是的,任何for循环都可以转换为whiledo-while循环。

例如:

for (初始化; 条件检查; 语句1) {
    // ......
}

这里的 语句1 => 通常是在条件检查中使用的变量的 增量减量

同样的等效 while 循环为:

初始化;
while (条件检查) {
     // .......
     语句1;
}

所以看起来你忘记初始化其中一个变量。另一个回答已经给了你那部分内容。

这个回答会帮助你将 for 循环映射到 while 循环,反之亦然。

希望能有所帮助。

英文:

Yes, any for loop can be converted into a while or do-while.

For example:

for(initialize; condition_check, statement1) {
    ......
}

Here statement1 => generally this is an increment or decrement of a variable used in the condition_check

Similarly the equivalent while loop would be:

initialize;
while (condition_check) {
     .......;
     statement1;
}

So it seems that you have forgotten to initialize one of the variables. The other answer has already given you that.

This answer would help in mapping the for loop to the while loop and vice-versa.

Hope it helps.

答案2

得分: 1

需要在第一个 `j_while` 结束后重置 `j` 计数器

    int i = 0, j = 0;
    while (i <= 7)
    {
    // 也可以在这里重置
    j=0;
        while (j <= 4)
        {
         if((i == 0 && j > 1) || (i == 1 && j > 0) || (i == 2 && j >= 0) || (i == 3 && j > 1) || (i > 3 && j > 1))
             System.out.print("1");
         else
             System.out.print(" ");
             j++;
         }
           System.out.println();
           i++;
     }
英文:

Need to reset j counter after first j_while end

int i = 0, j = 0;
while (i &lt;= 7)
{
//could also reset here
j=0;
    while (j &lt;= 4)
    {
     if((i == 0 &amp;&amp; j &gt; 1) || (i == 1 &amp;&amp; j &gt; 0) || (i == 2 &amp;&amp; j &gt;= 0) || (i == 3 &amp;&amp; j &gt; 1) || (i &gt; 3 &amp;&amp; j &gt; 1))
         System.out.print(&quot;1&quot;);
     else
         System.out.print(&quot; &quot;);
         j++;
     }
       System.out.println();
       i++;
 }

huangapple
  • 本文由 发表于 2020年4月10日 09:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61132886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定