简体   繁体   English

如何将垂直列表输出到水平列表中

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

相关问题 如何在 java 中将我的 output 从水平更改为垂直? - how do i change my output from horizontal to vertical in java? 如何将垂直和水平滚动条置于JScrollPane中心? - How do I center the vertical and horizontal scrollbars in a JScrollPane? 如何在 for 循环中以垂直直线打印 output - How do I print the output in a straight vertical line from for loop 如何将列表列表作为@RequestParam 传递? - How do I pass a list of lists as @RequestParam? 当我运行代码时,菜单显示为垂直而不是水平,该如何更改? - When i run my code the menus appear vertical not horizontal how do i change this? 如何在我的JTextArea(java)中添加水平和垂直滚动条 - how do I add a horizontal and vertical scroll bar to my JTextArea (java) 如何在ScrolledComposite内获取包含FlowLayout的Composite使其垂直而不是水平增长? - How do I get a Composite containing FlowLayout inside a ScrolledComposite to grow vertical and not horizontal? RecyclerView - 垂直滚动的水平列表 - RecyclerView - horizontal list with vertical scrolling 如何让我的 label 和组合框在 java 中显示为垂直而不是水平? 我尝试将布局设置为 null 但它隐藏了 label - How do I get my label and combo box to appear vertical instead of horizontal in java? I tried setting the layout to null but it hides the label 如何筛选完全垂直的尺寸和完全水平的尺寸? - How can I screen a full vertical size with exactly horizontal size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM