Java中for循环的错误输出

发布于 2024-10-26 15:12:56 字数 679 浏览 2 评论 0原文

这是我使用 for 循环编写的一个简单程序

public class Test
{
    public static void main (String[] args)
    {

        int high=10;

        for( int low =0; low <=high; low++){

            for (int mid=0; mid<=high; mid++)
            {
                System.out.print(mid);

            }
            System.out.println();    
        }
    }
}

,但我希望输出看起来

0 1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

2 3 4 5 6 7 8 9 10

etc...

10

像这样 0123456789100123456789100123456789100123456789100123456789100123456789100123456789100123 45678910012345678910012345678910012345678910

我做错了什么?

This is a simple program i wrote using for loop

public class Test
{
    public static void main (String[] args)
    {

        int high=10;

        for( int low =0; low <=high; low++){

            for (int mid=0; mid<=high; mid++)
            {
                System.out.print(mid);

            }
            System.out.println();    
        }
    }
}

But I want the output to look like

0 1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

2 3 4 5 6 7 8 9 10

etc...

10

Instead my output looks like this 012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910

What am i doing wrong?

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

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

发布评论

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

评论(8

自演自醉 2024-11-02 15:12:56

您没有打印出任何空格。

System.out.print(mid + " ");

编辑:

此外,每次通过内部循环时,您都会从 0 开始 mid,而不是从 low 开始。

You're not printing out any spaces.

System.out.print(mid + " ");

edit:

In addition, you're starting mid at 0 every time through the inner loop rather than starting it at low.

盛夏已如深秋| 2024-11-02 15:12:56
System.out.print(mid + " ");

应该

System.out.println(" ");

解决你的问题。

编辑:哦...好吧,还有:

for (int mid = low; mid<=high; mid++)
System.out.print(mid + " ");

and

System.out.println(" ");

Should fix you up.

EDIT: Oh ... well, that and:

for (int mid = low; mid<=high; mid++)
从此见与不见 2024-11-02 15:12:56

您可能需要在 System.out 调用中连接一个空格。

System.out.print( mid + " " );

You may want to concatenate a space to your System.out call.

System.out.print( mid + " " );
蓝色星空 2024-11-02 15:12:56

在内循环中设置 mid = low。

Set mid = low in the inner loop.

偷得浮生 2024-11-02 15:12:56

您想要设置 mid = low,并打印空格,正如其他人指出的那样:

int high=10;

for( int low =0; low <=high; low++)
    for (int mid=low; mid<=high; mid++)
    {
        System.out.print("%d ", mid);

    }
    System.out.println();
 }

编辑:删除了虚假的 \n。

You want to set mid = low, and print spaces as others have noted:

int high=10;

for( int low =0; low <=high; low++)
    for (int mid=low; mid<=high; mid++)
    {
        System.out.print("%d ", mid);

    }
    System.out.println();
 }

Edit: removed the spurious \n.

眸中客 2024-11-02 15:12:56

下面的代码就可以了

int high = 10;
    for (int low = 1; low <= high; low++) {
        for (int mid = low; mid <= high; mid++) {
            System.out.print(mid + " ");
        }
        System.out.println();
    }
}

Following code will do

int high = 10;
    for (int low = 1; low <= high; low++) {
        for (int mid = low; mid <= high; mid++) {
            System.out.print(mid + " ");
        }
        System.out.println();
    }
}
∞琼窗梦回ˉ 2024-11-02 15:12:56

我希望这能让你得到你想要的:

public class Test
{
    public static void main (String[] args)
    {

    int high=10;

    for( int low =0; low <=high; low++)
    {
        for (int mid=low; mid<=high; mid++) //start from low not 0
        {
            System.out.print(mid+" ");

        }
        System.out.println();

    }
    }
}

I hope this gets you what you want :

public class Test
{
    public static void main (String[] args)
    {

    int high=10;

    for( int low =0; low <=high; low++)
    {
        for (int mid=low; mid<=high; mid++) //start from low not 0
        {
            System.out.print(mid+" ");

        }
        System.out.println();

    }
    }
}
静若繁花 2024-11-02 15:12:56

试试这个

public class Test {
    public static void main (String[] args) {

    int high=10;

    for( int low =0; low<=high; low++) {

        for (int mid=low; mid<=high; mid++) {
            System.out.print(mid + " " );

        }

        System.out.println();
    }
  }
}

Try this

public class Test {
    public static void main (String[] args) {

    int high=10;

    for( int low =0; low<=high; low++) {

        for (int mid=low; mid<=high; mid++) {
            System.out.print(mid + " " );

        }

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