单个字符不';是否在Java中使用格式?
我在编程课上遇到的一个问题是要求我制作如下模式: 我使用了以下代码:单个字符不';是否在Java中使用格式?,java,string,format,Java,String,Format,我在编程课上遇到的一个问题是要求我制作如下模式: 我使用了以下代码: 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) +
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");
}
}
除了第一行数字外,我把所有的东西都打印出来了:我知道我可以使用if语句来解决这个问题,但我不想这样做,我想知道为什么它会这样做。问题是,新行插入到额外空格(“1\n”+空格)之前,需要删除减号(证明左对齐)并进行一些小的数学修改
1\n spaces (shown on next line to make it seem like your justifying right
2 1\n spaces
等等问题是新行插入到额外空格(“1\n”+空格)之前,需要删除减号(使左对齐)并进行一些小的数学修改
1\n spaces (shown on next line to make it seem like your justifying right
2 1\n spaces
依此类推trySystem.out.printf(“%10s%n”,num.substring(count,11))
trySystem.out.printf(“%10s%n”,num.substring(count,11))代码>