[英]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.