如何在控制台中输出多行角色图像

发布于 2025-01-29 03:50:06 字数 1712 浏览 3 评论 0原文

我正在开发基于文本的游戏,我想让标题吸引人。我试图将文本转换为ASCII转换器来制作一个漂亮的标题,然后将其复制并粘贴到我的代码中以输出它,但它不起作用。

这是我试图做的:

System.out.println("
██████╗ ██╗   ██╗███╗   ██╗ ██████╗ ███████╗ ██████╗ ███╗   ██╗     ██████╗ ███████╗                            
██╔══██╗██║   ██║████╗  ██║██╔════╝ ██╔════╝██╔═══██╗████╗  ██║    ██╔═══██╗██╔════╝                            
██║  ██║██║   ██║██╔██╗ ██║██║  ███╗█████╗  ██║   ██║██╔██╗ ██║    ██║   ██║█████╗                              
██║  ██║██║   ██║██║╚██╗██║██║   ██║██╔══╝  ██║   ██║██║╚██╗██║    ██║   ██║██╔══╝                              
██████╔╝╚██████╔╝██║ ╚████║╚██████╔╝███████╗╚██████╔╝██║ ╚████║    ╚██████╔╝██║                                 
╚═════╝  ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝  ╚═══╝     ╚═════╝ ╚═╝                                 
                                                                                                                
    ████████╗██╗  ██╗███████╗    ███████╗ ██████╗ ██████╗  ██████╗  ██████╗ ████████╗████████╗███████╗███╗   ██╗
    ╚══██╔══╝██║  ██║██╔════╝    ██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔═══██╗╚══██╔══╝╚══██╔══╝██╔════╝████╗  ██║
       ██║   ███████║█████╗      █████╗  ██║   ██║██████╔╝██║  ███╗██║   ██║   ██║      ██║   █████╗  ██╔██╗ ██║
       ██║   ██╔══██║██╔══╝      ██╔══╝  ██║   ██║██╔══██╗██║   ██║██║   ██║   ██║      ██║   ██╔══╝  ██║╚██╗██║
       ██║   ██║  ██║███████╗    ██║     ╚██████╔╝██║  ██║╚██████╔╝╚██████╔╝   ██║      ██║   ███████╗██║ ╚████║
       ╚═╝   ╚═╝  ╚═╝╚══════╝    ╚═╝      ╚═════╝ ╚═╝  ╚═╝ ╚═════╝  ╚═════╝    ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═══╝");

但似乎没有用。我知道它在JavaScript中起作用,但我想知道它是否也可以在Java中使用。

I am developing a text-based game and I want to make the title something catchy. I tried to use a text to ASCII converter to make a nice-looking title and then copy and paste it in my code to output it, but it didn't work.

Here is what I tried to do:

System.out.println("
██████╗ ██╗   ██╗███╗   ██╗ ██████╗ ███████╗ ██████╗ ███╗   ██╗     ██████╗ ███████╗                            
██╔══██╗██║   ██║████╗  ██║██╔════╝ ██╔════╝██╔═══██╗████╗  ██║    ██╔═══██╗██╔════╝                            
██║  ██║██║   ██║██╔██╗ ██║██║  ███╗█████╗  ██║   ██║██╔██╗ ██║    ██║   ██║█████╗                              
██║  ██║██║   ██║██║╚██╗██║██║   ██║██╔══╝  ██║   ██║██║╚██╗██║    ██║   ██║██╔══╝                              
██████╔╝╚██████╔╝██║ ╚████║╚██████╔╝███████╗╚██████╔╝██║ ╚████║    ╚██████╔╝██║                                 
╚═════╝  ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝  ╚═══╝     ╚═════╝ ╚═╝                                 
                                                                                                                
    ████████╗██╗  ██╗███████╗    ███████╗ ██████╗ ██████╗  ██████╗  ██████╗ ████████╗████████╗███████╗███╗   ██╗
    ╚══██╔══╝██║  ██║██╔════╝    ██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔═══██╗╚══██╔══╝╚══██╔══╝██╔════╝████╗  ██║
       ██║   ███████║█████╗      █████╗  ██║   ██║██████╔╝██║  ███╗██║   ██║   ██║      ██║   █████╗  ██╔██╗ ██║
       ██║   ██╔══██║██╔══╝      ██╔══╝  ██║   ██║██╔══██╗██║   ██║██║   ██║   ██║      ██║   ██╔══╝  ██║╚██╗██║
       ██║   ██║  ██║███████╗    ██║     ╚██████╔╝██║  ██║╚██████╔╝╚██████╔╝   ██║      ██║   ███████╗██║ ╚████║
       ╚═╝   ╚═╝  ╚═╝╚══════╝    ╚═╝      ╚═════╝ ╚═╝  ╚═╝ ╚═════╝  ╚═════╝    ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═══╝");

But it didn't seem to work. I know it works in JavaScript, but I was wondering if it would also work in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-02-05 03:50:06

字符串

文字 - 是一个字符串由双引号中包含的零或多个字符组成“ myText”

正是您尝试过它,就无法在Java中创建多行字母字体。根据语言规范,这是一个编译时错误,线侧端终止。

String invalidLiteral = "line1
                         line2
                         ..."; // will cause a compilation error

为了制作这样的字符串文字编译,可以用新的行字符\ n替换行终止。

String validString = "line1\nline2\nline3";

为了使其可读性,结果的字符串可能会变得很长每个部分。

String validConcatinatedString = "line1\n" +
                                 "line2\n" +
                                 "line3";

尽管它会起作用,但可能很乏味。

带有Java 15的文本块

您可以使用文本块可用。

为了创建文本块,您需要将目标多行文本包装在三重双引号字符“”“”“

String myASCIIArt = """ // no text after the opening delimiter
              Your amazing 
                  ASCII art 
                       here""";

。 “ 应立即为 ,然后是线终止,并且文本块的实际主体始终在下一行开始。

String literals

String literal - is a string consists of zero or more characters enclosed in double quotes "myText".

It is not possible to create a multiline string literal in Java, precisely as you've tried it. According to the language specification it's a compile-time error, line termination is present in a string lateral.

String invalidLiteral = "line1
                         line2
                         ..."; // will cause a compilation error

To make such string literal compile, line termination can be replaced with a new line character \n.

String validString = "line1\nline2\nline3";

The resulting string can become very long, in order to make it readable you can split the string into chunks concatenated with a plus sign +, appending a new line character \n to each chunk.

String validConcatinatedString = "line1\n" +
                                 "line2\n" +
                                 "line3";

Although it'll work, it might be tedious.

Text Blocks

With Java 15 you can create a multiline string using text blocks available.

In order to create a text block, you need to enclose the target multiline text in triple double-quote characters """.

String myASCIIArt = """ // no text after the opening delimiter
              Your amazing 
                  ASCII art 
                       here""";

Note that the opening delimiter """ should be immediately followed by the line termination, and the actual body of the text block always starts on the next line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文