[英]How do I output the vertical list into horizontal lists
public class NormalNumbers {
public static void main(String[] args) {
int x = 1;
while ((x >= 1) && (x <= 100)) {
System.out.println("x = " + x);
x = x + 1;
}
}
}
The current output is: 目前的输出是:
x = 1
x = 2
...
x = 100
I want to change the format to: 我想将格式更改为:
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
and so on. 等等。
How do I achieve that? 我如何实现这一目标?
System.out.println
prints the text and adds a new line. System.out.println
打印文本并添加新行。 Use System.out.print
to print on the same line instead. 请使用System.out.print
在同一行上打印。
So it would be something like this: 所以它会是这样的:
System.out.print("x=" + x + " ");
To add a new line each 5 numbers, use: 要为每5个数字添加一个新行,请使用:
// if x is multiple of 5, add a new line
if (x % 5 == 0) {
System.out.println();
}
PD: You can use x++
( increment operator ) or x += 1
(in the case you want to increase in more than one unit) instead of x = x + 1
. PD:您可以使用x++
( 增量运算符 )或x += 1
(如果要增加多个单位),而不是x = x + 1
。
PD2 : You might want to use a tabulation ( \\t
) instead of a space for separating your numbers. PD2:您可能希望使用制表符( \\t
)而不是用于分隔数字的空格。 That way, numbers with two digits will have the same indentation than numbers with one digit. 这样,具有两位数的数字将具有与具有一位数的数字相同的缩进。
System.out.print("x=" + x + "\t");
Instead of using println()
, which automatically inserts a newline character at the end of whatever you're printing, just use print()
and add an extra space to pad your entries. 而不是使用println()
,它会在您打印的任何内容的末尾自动插入换行符,只需使用print()
并添加额外的空格来填充您的条目。
If you want to inject a newline after 5
entries specifically, you can do so with an empty println()
and the modulus operator like so: 如果你想在5
个条目之后专门注入换行符,你可以使用空的println()
和模数运算符这样做:
while ((x >= 1) && (x <= 100)) {
System.out.print("x = " + x);
if (x % 5 == 0) {
System.out.println();
}
x = x + 1;
}
Divide your counter by 5 using modulus division if there is no remainder then create a new line: 如果没有余数,则使用模数除法将计数器除以5,然后创建一个新行:
int x = 1;
while ((x >= 1) && (x <= 100))
{
System.out.print("x = " + x + " ");
if(x % 5 == 0)
{
System.out.print("\n");
}
x = x + 1;
}
println
is next line, print
is on the same line. println
是下一行, print
在同一行。 x % 5 == 0
checks that the x values is a multiple of 5 or not. x % 5 == 0
检查x值是否为5的倍数。
int x = 1; while ((x >= 1) && (x <= 100)) { if (x % 5 == 0) { System.out.println("x="+x); } else { System.out.print("x=" +x+ " "); } x = x + 1; }
That gives you output as 这会给你输出
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
x=11 x=12 x=13 x=14 x=15
x=16 x=17 x=18 x=19 x=20
-----
I think that in your case the better way is using for(;;)
statement: 我认为在你的情况下更好的方法是使用for(;;)
语句:
for (int x = 1; x > 0 && x < 101;)
System.out.print("x = " + x + (x++ % 5 == 0 ? "\n" : " "));
The ternary operator x++ % 5 == 0 ? "\\n" : " "
三元运算符x++ % 5 == 0 ? "\\n" : " "
x++ % 5 == 0 ? "\\n" : " "
is responsible for the new line and increment the x
variable. x++ % 5 == 0 ? "\\n" : " "
负责新行并增加x
变量。
Output: 输出:
x = 1 x = 2 x = 3 x = 4 x = 5
x = 6 x = 7 x = 8 x = 9 x = 10
...
x = 96 x = 97 x = 98 x = 99 x = 100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.