while 循环输出不完整

发布于 2024-11-08 01:22:37 字数 866 浏览 0 评论 0原文

我应该给出这个输出

* * * * *
 * * * * *
* * * * * *
 * * * * *

等等 5 次迭代 但它只显示前 2 个输出

这是我的代码

public class itiration {

    public static void main( String args[]){

        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;

        while(counter1<=5)
        {

                while(counter2<=5)
                {
                    System.out.print("* ");
                    System.out.print(" ");
                    counter2++;
                }

            System.out.println();

                while(counter3<=5)
                {
                    System.out.print(" ");
                    System.out.print("* ");
                    counter3++;
                }


            System.out.println();

            counter1++;
        }

    }

}

这不是作业

I am supposed to give this output

* * * * *
 * * * * *
* * * * * *
 * * * * *

so on and so forth 5 itirations
but it only shows the first 2 output

here's my code

public class itiration {

    public static void main( String args[]){

        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;

        while(counter1<=5)
        {

                while(counter2<=5)
                {
                    System.out.print("* ");
                    System.out.print(" ");
                    counter2++;
                }

            System.out.println();

                while(counter3<=5)
                {
                    System.out.print(" ");
                    System.out.print("* ");
                    counter3++;
                }


            System.out.println();

            counter1++;
        }

    }

}

this is not a homework

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2024-11-15 01:22:37

您是否尝试过使用调试器单步调试该程序?

提示:外循环执行第一次迭代后,counter2 和 counter3 的值是多少?

Have you tried stepping through this program with a debugger?

HINT: After the outer loop executes its first iteration, what are the values of counter2 and counter3?

黑寡妇 2024-11-15 01:22:37

您需要在循环中重置 counter2counter3(例如在 counter1++ 之后),否则它们在第一次运行后将保持在值 5循环的内部循环将不再运行。

You need to reset counter2 and counter3 in the loop (after counter1++ for example), otherwise they'll stay at value 5 after the first run of the loop, and the inner loops will not run any more.

红ご颜醉 2024-11-15 01:22:37

您不会为主循环的每次迭代重置 counter2 和 counter3。
试试这个:

    int counter1 = 1;
    while(counter1<=5)
    {        
        int counter2 = 1;
        int counter3 = 1;

You're not resetting counter2 and counter3 for each iteration of the main loop.
Try this:

    int counter1 = 1;
    while(counter1<=5)
    {        
        int counter2 = 1;
        int counter3 = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文