简体   繁体   English

如何以Java格式打印出此ASCII卡

[英]How do I print out this ASCII card in Java, Formatted

I am new to java and right now I am making a black-jack game. 我是Java新手,现在正在制作二十一点游戏。 I want to imitate the real game as much as possible instead of printing out numbers and letters, therefore I made a little ASCII card in wordpad. 我想尽可能地模仿真实的游戏,而不是打印数字和字母,因此我在写字板中做了一点ASCII卡。 The problem is, it does not fit inside the "" when I try to print it, and it does not work. 问题是,当我尝试打印它时,它不适合在“”中,并且不起作用。

As I said, I am new to Java. 正如我所说,我是Java的新手。 Can someone please help me? 有人可以帮帮我吗? Thank you. 谢谢。

Here is the card I want to print out: 这是我要打印的卡:

 .-------.  
 | ** ** |  
 |*  *  *|  
 | *   * |  
 |   *   |  
 |   J   |  
 '-------'  

This is what I tried: 这是我尝试的:

String test
 =  ".-------.  
     | ** ** |  
     |*  *  *|  
     | *   * |  
     |   *   |  
     |   J   |  
     '-------'"

You could make use of StringBuilder like so: 您可以像这样使用StringBuilder

StringBuilder sb = new StringBuilder();
sb.append(".-------.").append(System.lineSeparator());    //System.lineSeparator() gets us the new line character which is used by the underlying OS.
sb.append("| ** ** |").append(System.lineSeparator());
sb.append("|*  *  *|").append(System.lineSeparator());
sb.append("| *   * |").append(System.lineSeparator());
...

Then, you print it like so: System.out.println(sb.toString()); 然后,按如下方式进行打印: System.out.println(sb.toString()); .

Strictly speaking, you could do it through string concatenation: 严格来说,您可以通过字符串连接来实现:

String str = ".-------." + System.lineSeparator() + ...

However, concatenating long strings is usually considered bad practice, which is where the StringBuilder comes in. 但是,串联长字符串通常被认为是不好的做法,这是StringBuilder出现的地方。

you can do like this : 你可以这样:

    String test
     =   ".-------."+ "\n"+
         "| ** ** |"+ "\n"+
         "|*  *  *|"+ "\n"+ 
         "| *   * |"+ "\n"+ 
         "|   *   |"+ "\n"+ 
         "|   J   |"+ "\n"+ 
         "'-------'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM