格式化文本输出Java SE

我试图从Java中的System.out.println打印一些ASCII艺术。 当我这样做时,虽然源代码中的所有间距都消失了。 打印出来的是一堆废话,没有任何意义。 图1显示了源代码的外观以及ASCII艺术应该是什么样子。 图2显示了程序运行时横幅的实际外观。

我的问题是如何按预期打印ASCII艺术? 如何使用使横幅可读的格式打印ASCII艺术品?

例如,使用Python,我可以通过使用三引号轻松完成这项工作。 Java有类似的文本机制吗?

图1:ASCII Art的外观,源代码中的内容。 图1 :ASCII艺术应该是什么样子

在此处输入图像描述 图2 :运行程序时ASCII艺术的样子

请参阅我的源代码如下:

package Boring; import java.util.Scanner; public class EnchantedBoringLevel { public EnchantedBoringLevel () { System.out.println("\n\n\n\n\n\n\n\n\n\n\n"); /* System.out.println("\n" + "EEEE htd BBBB FFFF t " + "E htd BB ii F t " + "EEE nnn ccc hhh aa nnn ttt eee ddd BBBB ooo rrr nnn ggg FFF ooo rrr eee ss ttt " + "E nnchhaannteedd BB oor ii nngg F ooreest " + "EEEE nn ccc hh aaa nn tt ee ddd BBBB ooo r ii nn ggg F ooo r ee ss tt " + " g " + " ggg " ); */ System.out.println("***Enchanted Boring Forest***"); System.out.println("Welcome to another boring level of this video game!\n\n\n\n"); this.enchant(); } public void enchant() { System.out.println("You find yourself in a magical forest all alone.\nYou hear music in the distance.\n" + "An elf appears and takes you to their tree house mansion."); System.out.print("The elf prince offers you three choices 1) invisible ring, 2) galactic sunglasses, 3) a map of the enchanted forest. Which do you choose?"); Scanner enchanted_choice = new Scanner(System.in); String choice; choice = enchanted_choice.next(); switch(new Integer(choice)) { case 1: System.out.println("You chose the invisible ring"); new BoringInvisibleRing(); break; case 2: System.out.println("You chose the galactic sunglasses"); new BoringGalacticSunglasses(); break; case 3: System.out.println("You chose the map of the enchanted forest"); break; default: System.out.println("You shot yourself in the face."); } } } 

控制台包装您的文本。 在控制台设置中设置更高的列号

问题是java将您的文本视为单行并且cmd将其包装,因此它变得不可读。 要创建新行,请使用换行符\n