Java:使System.out.println()参数跨越多行?

时间:2014-12-10 19:05:15

标签: java

我有以下消息:

System.out.println("Players take turns marking a square. Only squares not already marked can be picked. Once a player has marked three squares in a row, he or she wins! If all squares are marked and no three squares are the same, a tied game is declared. Have Fun!");

这是一个非常长的消息,并在我的屏幕上划线,我想将其分解成段,这样就不会破坏我的代码的设计流程。当我按Enter键时,Java不再将该行解释为字符串; Java认为这是一个单独的声明。如何让Java解释多行打印语句?

5 个答案:

答案 0 :(得分:10)

你正在寻找这样的东西吗?

String line = "Information and stuff" + 
          "More information and stuff " + 
          "Even more information and stuff";

System.out.println(line);

答案 1 :(得分:0)

添加' \ n'你想要一条新线路。

答案 2 :(得分:0)

您可以输入:System.out.println("这是第一行\ n" +"这是第二行")

答案 3 :(得分:0)

遵循Java的编码约定:

System.out.println("Players take turns marking a square. "
                   + "Only squares not already marked can be picked. "
                   + "Once a player has marked three squares in a row, "
                   + "he or she wins! If all squares are marked and no "
                   + "three squares are the same, a tied game is declared. "
                   + "Have Fun!");

为了便于阅读,请始终与串联运算符一起中断以开始下一行。

https://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248

希望有帮助!

Brady

答案 4 :(得分:0)


System.out.println("Players take turns marking a square."
+ "\nOnly squares not already marked can be picked."
+ "\nOnce a player has marked three squares in a row, he or she wins!"
+ "\nIf all squares are marked and no three squares are the same, a tied game is declared."
+ "\nHave Fun!");

完成。