我对如何使用printf有疑问。就我而言,我不知道如何使用printf使我的表格(二维数组)与表格的对齐方式相匹配。我想要它,以便标题数字符合表格中的数字。我怎样才能解决这个问题?任何帮助将不胜感激。以下是我的输出:
run:
Projectile Distance (Feet)
MPH 572.95780 630.25357 687.54935 744.84513 802.14091 2291.83118
____________________________________________________________________________________
45 42.3390045 44.8135645 38.8776945 25.6454145 7.6001045 44.20434
67 63.0380667 66.7224167 57.8845667 38.1831767 11.3157167 65.81535
77 72.4467377 76.6809877 66.5240577 43.8821577 13.0046277 75.63853
89 83.7371389 88.6312689 76.8914489 50.7209389 15.0313289 87.42636
90 84.6779990 89.6271290 77.7553890 51.2908390 15.2002190 88.40867
56 52.6885356 55.7679956 48.3811356 31.9142956 9.4579156 55.00984
99 93.1457999 98.5898399 85.5309299 56.4199199 16.7202399 97.24954
BUILD SUCCESSFUL (total time: 0 seconds)
以下是我的两个文件的代码:
public class CatapultTester {
static int velocity[] = {45, 67, 77, 89, 90, 56, 99};
static double angle[] = {Math.toDegrees(10), Math.toDegrees(11), Math.toDegrees(12),
Math.toDegrees(13), Math.toDegrees(14), Math.toDegrees(40)};
public static void main(String args[]){
System.out.printf("%55s", "Projectile Distance (Feet)");
System.out.println("\n");
System.out.printf("%1s", "MPH");
for(int k = 0; k < angle.length; k++){
System.out.printf("%12.5f", angle[k]);
}
System.out.println();
System.out.println("___________________________________________________"
+ "_________________________________");
System.out.println();
Catapult.display(angle, velocity);
}
}
_
public class Catapult {
public static void display(double angle[], int velocity[]){
double[][] result = new double[velocity.length][angle.length];
for(int i = 0; i < velocity.length; i++){
for(int j = 0; j < angle.length; j++){
result[i][j] = velocity[i] * Math.sin(angle[j] / 9.8);
System.out.printf("%1d%12.5f", velocity[i], result[i][j]);
}
System.out.println();
}
}
}
答案 0 :(得分:1)
在display
方法中,您要为一行中的每个元素打印 MPH 值,您必须只打印一次:
public static void display(double angle[], int velocity[])
{
double[][] result = new double[velocity.length][angle.length];
for (int i = 0; i < velocity.length; i++) {
System.out.print(velocity[i] + " "); // Print once per line, and add extra space to align correctly
for (int j = 0; j < angle.length; j++) {
result[i][j] = velocity[i] * Math.sin(angle[j] / 9.8);
System.out.printf("%12.5f", result[i][j]);
}
System.out.println();
}
}
<强>输出:强>
Projectile Distance (Feet)
MPH 572,95780 630,25357 687,54935 744,84513 802,14091 2291,83118
____________________________________________________________________________________
45 42,33900 44,81356 38,87769 25,64541 7,60010 44,20434
67 63,03806 66,72241 57,88456 38,18317 11,31571 65,81535
77 72,44673 76,68098 66,52405 43,88215 13,00462 75,63853
89 83,73713 88,63126 76,89144 50,72093 15,03132 87,42636
90 84,67799 89,62712 77,75538 51,29083 15,20021 88,40867
56 52,68853 55,76799 48,38113 31,91429 9,45791 55,00984
99 93,14579 98,58983 85,53092 56,41991 16,72023 97,24954
答案 1 :(得分:0)
您的标题循环为每个数字使用%12.5f格式
您的显示方法循环使用%1d%12.5f格式表示两个数字。匹配它们,它们应该更好地对齐。