print语句-Java语法数组元素
我正在打印print语句-Java语法数组元素,java,printf,Java,Printf,我正在打印 do { System.out.print("arrPascal["+i+"]["+j+"] = "); System.out.println(arrPascal[i][j]);) fibo += arrPascal[i][j]; i--; j++; } 但是我的代码会导致 arrPascal[58][9] = 10648873950\n arrPascal[57][1
do {
System.out.print("arrPascal["+i+"]["+j+"] = ");
System.out.println(arrPascal[i][j]);)
fibo += arrPascal[i][j];
i--;
j++;
}
但是我的代码会导致
arrPascal[58][9] = 10648873950\n
arrPascal[57][10] = 43183019880\n
如何确保该代码为
的所有结果排列“=”(“arrPascal[“+i+”][“+j+”]=”)
您可以使用带有宽度说明符的System.out.format
。您可以硬编码大于所需任何可能宽度的宽度,也可以根据需要打印的任何字符串的最大长度计算宽度
arrPascal[58][9] = 10648873950\n
arrPascal[57][10] = 43183019880\n
使用System.out.format(…)
而不是System.out.print(…)
。它可以更好地控制元素的宽度。检查
System.out.format("%-19s = %d%n", "arrPascal[" + i +"][" + j + "]", arrPascal[i][j]);
//width is 19, left aligned