gpt4 book ai didi

java - 如何在java中使用printf

转载 作者:行者123 更新时间:2023-12-01 13:39:49 25 4
gpt4 key购买 nike

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

关于java - 如何在java中使用printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com