如何在java中使用printf

如何在java中使用printf,java,printf,Java,Printf,我有一个关于如何使用printf的问题。在我的例子中,我不知道如何使用printf使表的标题(二维数组)与表的对齐方式匹配。我想要它,以便标题编号与表中的编号一致。我怎样才能解决这个问题?任何帮助都将不胜感激。以下是我的输出: run: Projectile Distance (Feet) MPH 572.95780 630.25357 687.54935 744.84513 802.14091 2291.83118 __

我有一个关于如何使用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);

}

}
公共级弹射器{
静态整数速度[]={45,67,77,89,90,56,99};
静态双角度[]={Math.toDegrees(10),Math.toDegrees(11),Math.toDegrees(12),
数学toDegrees(13)、数学toDegrees(14)、数学toDegrees(40)};
公共静态void main(字符串参数[]){
System.out.printf(“%55s”,“射弹距离(英尺)”);
System.out.println(“\n”);
系统输出打印F(“%1s”,“MPH”);
对于(int k=0;k
_

公共级弹射器{
公共静态空白显示(双角度[],整数速度[]){
双精度[]结果=新双精度[速度.长度][角度.长度];
对于(int i=0;i
标题循环对每个数字使用%12.5f格式


显示方法循环对两个数字使用%1d%12.5f格式。匹配它们,它们应该对齐得更好。

显示
方法中,要打印一行中每个元素的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();
    }
}
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