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

新手上路,请多包涵

我有以下消息:

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

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

原文由 chopper draw lion4 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 463
2 个回答

你在找这样的东西吗?

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

System.out.println(line);

原文由 DejaVuSansMono 发布,翻译遵循 CC BY-SA 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

原文由 bradylange 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题