Print in new line, java

2019-01-10 17:25发布

I have following code :

    System.out.println(" | 1  2  3  4  5  6  7  8  9");
    System.out.println("----------------------------");
    System.out.println("");

I use println to create a new line. Is it possible to do the same using \n or \r? I tried to add \n to the second println statment and continue printing with the print method but \n does not create a new line.

any ideas?

11条回答
欢心
2楼-- · 2019-01-10 17:36
System.out.println("hello"+"\n"+"world");
查看更多
狗以群分
3楼-- · 2019-01-10 17:36

"\n" this is the simple method to separate the continuous String

查看更多
来,给爷笑一个
4楼-- · 2019-01-10 17:39

It does create a new line. Try:

System.out.println("---\n###");
查看更多
再贱就再见
5楼-- · 2019-01-10 17:39

Your best shot would be with

String.format("%n")

or

System.out.printf("%n");

It is supposed to print a newline character, depending on the current platform, so it's perfect for the console.

If you are printing to a file, then it depends.

查看更多
相关推荐>>
6楼-- · 2019-01-10 17:39

Since you are on Windows, instead of \n use \r\n (carriage return + line feed).

查看更多
够拽才男人
7楼-- · 2019-01-10 17:44
//Case1:
System.out.println(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");

//Case2:
System.out.printf(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");

//Case3:
System.out.print(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");
查看更多
登录 后发表回答