简体   繁体   English

如何在 for 循环中以垂直直线打印 output

[英]How do I print the output in a straight vertical line from for loop

I used a for-loop in my code and my output prints vertically, but what I want is it to print horizontally.我在我的代码中使用了一个 for 循环,我的 output 是垂直打印的,但我想要的是水平打印。 For example例如

for (int i = 0; i < 10; i++ ) {
    System.out.println("i is " + (i+1) + " | ");
}

For this the output is:为此,output 是:

i is 1 |
i is 2 |
i is 3 |
and so on... 

The output I want is:我要的output是:

i is 1 | i is 2 | i is 3 | ... and so on

Change it to System.out.print as below:将其更改为 System.out.print,如下所示:

for (int i = 0; i < 10; i++ ) {
      System.out.print("i is " + (i+1) + " | ");
}

That should do.应该这样做。

Use only print() function instead of println()仅使用print() function 而不是println()
Follow below code:按照下面的代码:

for (int i = 0; i < 10; i++ ) {
    System.out.print("i is " + (i+1) + " | ");
}

Use System.out.print instead of System.out.println because println prints at new line.使用System.out.print而不是System.out.println因为println在新行打印。

for (int i = 0; i < 10; i++) {
        System.out.print("i is " + (i + 1) + " | ");
}

Println prints a new line while print does not Println 打印一个新行而 print 不打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM