打印声明,间距问题。

时间:2018-04-20 21:59:05

标签: java printf

我正在尝试创建基于文本的老虎机。但是,当我打印变量" s1" " s2"或" s3",正如你所看到的,一切都会转移。我的问题是由s1引起的,可能是各种各样的长度。我熟悉printf概念,但我不确定如何纠正间距问题。

                  ("|=================================|     ____\n" +
                   "|           SLOT MACHINE          |    [    ]\n" +
                   "|=================================|     / /\n" +
                   "|                                 |    / /\n" +
                   "|    =========================    |   / /\n" +
                   "|    |   1   |   2   |   3   |    |  / /\n" +
                   "|    =========================    | / /\n" +
                   "|    |       |       |       |    |===|\n"+
                   "|    | "+s1+"| "+s2+"| "+s3+"|    |   |\n" +
                   "|    |       |       |       |    |===|\n" +
                   "|    =========================    |\n" +
                   "|                                 |\n" +
                   "|          [Insert Money]         |\n" +
                   "|=================================|" );

输出看起来像这样......

|=================================|     ____
|           SLOT MACHINE          |    [    ]
|=================================|     / /
|                                 |    / /
|    =========================    |   / /
|    |   1   |   2   |   3   |    |  / /
|    =========================    | / /
|    |       |       |       |    |===|
|    | Apple| Orange| Orange|    |   |
|    |       |       |       |    |===|
|    =========================    |
|                                 |
|          [Insert Money]         |
|=================================|

1 个答案:

答案 0 :(得分:1)

替换为以下内容。应该工作正常。

String slotMachine = "|=================================|     ____\n" +
                     "|           SLOT MACHINE          |    [    ]\n" +
                     "|=================================|     / /\n" +
                     "|                                 |    / /\n" +
                     "|    =========================    |   / /\n" +
                     "|    |   1   |   2   |   3   |    |  / /\n" +
                     "|    =========================    | / /\n" +
                     "|    |       |       |       |    |===|\n"+
                     "|    |%7s|%7s|%7s|    |   |\n" +
                     "|    |       |       |       |    |===|\n" +
                     "|    =========================    |\n" +
                     "|                                 |\n" +
                     "|          [Insert Money]         |\n" +
                     "|=================================|";
System.out.printf(slotMachine, "Apple", "Orange", "Orange");