簡體   English   中英

如何在Java中使用printf

[英]how to use printf in java

我對如何使用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();
    }
}
}

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

標頭循環為每個數字使用%12.5f格式

您的顯示方法循環將%1d%12.5f格式用於兩個數字。 匹配它們,它們應該更好地對齊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM