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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27408889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:19:15  来源:igfitidea点击:

Java: Making System.out.println() argument span multiple lines?

java

提问by chopper draw lion4

I have the following message:

我有以下消息:

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!");

This is a very long message and streaks across my screen, I want to break it up into segments so it does not break the design flow of my code. When I press enter though, Java no longer interprets the line as a string; Java believes it is a separate statement. How can I make Java interpret multiline print statements?

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

采纳答案by DejaVuSansMono

Are you looking for something like this?

你在寻找这样的东西吗?

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

System.out.println(line);

回答by RAEC

add '\n' where ever you want a new line.

在你想要换行的地方添加 '\n'。

回答by Vlad

You can type: System.out.println("This is the first line \n" + "This is the second line")

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

回答by bradylange

Following Java's coding conventions:

遵循 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!");

Always break with the concatenation operator to start the next line for readability.

始终与连接运算符断开以开始下一行以提高可读性。

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

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

Hope that helps!

希望有帮助!

Brady

布雷迪