单个字符在Java中不使用格式左对齐吗?

时间:2017-02-13 02:03:25

标签: java string format

我在编程课程中遇到的一个问题是要求我制作这样的模式: pattern4

我使用了以下代码:

public static void ch5ex18c() {
    System.out.println("Pattern C");
    String num = "6 5 4 3 2 1";
    for (int count = 10; count >= 0; count-=2){
        System.out.printf("%-10s", num.substring(count, 11) + "\n");
    }
}

除了第一个数字行外,我得到了所有打印好的内容:
problem

我知道我可以使用if语句解决这个问题,但我不想这样做,我想知道它为什么会这样做。

1 个答案:

答案 0 :(得分:0)

问题是你的新行是在额外空格(“1 \ n”+空格)之前插入的,你需要删除减号(左对齐)并进行一些小的数学修改。

1\n spaces (shown on next line to make it seem like your justifying right
2 1\n spaces

等等