打印值表

时间:2014-12-29 06:18:46

标签: java

我必须打印一个包含7行和6列的值表。请运行计算机上的程序,以便查看输出的内容。您将如何修复代码,以便所有值都能整齐地放入表中?

public class Catapult {
    Catapult() {

    }

    public double[][] calcTrajectory(int ROWS, int COLS, int[] MPH, int[] degrees, double[][] trajectories) {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                trajectories[row][col] = Math.pow(MPH[row], 2) * Math.sin(2 * degrees[col]) / 9.8;
            }

        }
        return trajectories;
    }

    public static void printScores(double[][] trajectories, int ROWS, int COLS, int[] MPH) {
        System.out.println("                    Projectile Distance (feet)");
        System.out.println("   MPH      25 deg      30 deg      35 deg      35 deg      40 deg      45 deg");
        System.out.println("=====================================================================");

        for (int row = 0; row < ROWS; row++) {
            System.out.printf("%5d", MPH[row]);
            for (int col = 0; col < COLS; col++) {
                System.out.printf("%13.2f", trajectories[row][col]);
            }
            System.out.println();
        }

    }
}

public class CatapultTester
{
    static final int ROWS = 7;
    static final int COLS = 6;

    public static void main(String[] args)
    {
        Catapult object = new Catapult();
        int degrees[] = { 25, 30, 35, 40, 45, 50 };
        int MPH [] = { 20, 25, 30, 35, 40, 45, 50};
        double[][] trajectories = new double[ROWS][COLS];
        trajectories = object.calcTrajectory(ROWS, COLS, MPH, degrees, trajectories);
        object.printScores(trajectories, ROWS, COLS, MPH);
    }
}

这是输出:

                   Projectile Distance (feet)
     MPH      25 deg      30 deg      35 deg      35 deg      40 deg      45 deg
 =============================================================================
     20
     25
     30
     35
     40
     45
     50
            -10.71       -12.44        31.59       -40.57        36.49       -20.67
            -16.73       -19.44        49.36       -63.39        57.02       -32.29
            -24.10       -27.99        71.07       -91.28        82.10       -46.50
            -32.80       -38.10        96.74      -124.24       111.75       -63.30
            -42.84       -49.76       126.35      -162.27       145.96       -82.67
            -54.22       -62.98       159.91      -205.37       184.73      -104.63
            -66.93       -77.76       197.42      -253.54       228.06      -129.17

2 个答案:

答案 0 :(得分:2)

改变这个:

for (int row = 0; row < ROWS; row++) {
    System.out.printf("%5d\n", MPH[row]);
}
for (int row = 0; row < ROWS; row++) {
    for (int col = 0; col < COLS; col++) {
        System.out.printf("%13.2f", trajectories[row][col]);
    }
    System.out.println();
}

到此:

for (int row = 0; row < ROWS; row++) {
    System.out.printf("%5d", MPH[row]);
    for (int col = 0; col < COLS; col++) {
        System.out.printf("%13.2f", trajectories[row][col]);
    }
    System.out.println();
}

这是输出:

                    Projectile Distance (feet)
   MPH      25 deg      30 deg      35 deg      35 deg      40 deg      45 deg
=====================================================================
   20       -10.71       -12.44        31.59       -40.57        36.49       -20.67
   25       -16.73       -19.44        49.36       -63.39        57.02       -32.29
   30       -24.10       -27.99        71.07       -91.28        82.10       -46.50
   35       -32.80       -38.10        96.74      -124.24       111.75       -63.30
   40       -42.84       -49.76       126.35      -162.27       145.96       -82.67
   45       -54.22       -62.98       159.91      -205.37       184.73      -104.63
   50       -66.93       -77.76       197.42      -253.54       228.06      -129.17

您的问题来自\n System.out.printf("%5d\n", MPH[row]);

答案 1 :(得分:1)

public static void printScores(double[][] trajectories, int ROWS, int COLS, int[] MPH){
   System.out.println("                    Projectile Distance (feet)");
   System.out.println("   MPH      25 deg      30 deg      35 deg      35 deg      40 deg          45 deg");
   System.out.println("=============================================================================="); 


   for(int row = 0; row < ROWS; row++){
       System.out.printf("%5d", MPH[row]);
       for(int col = 0; col < COLS; col++){
          System.out.printf("%13.2f", trajectories[row][col]);
       }
       System.out.println();
   }  
}

Out put

    MPH      25 deg      30 deg      35 deg      35 deg      40 deg      45 deg
==============================================================================
20       -10.71       -12.44        31.59       -40.57        36.49       -20.67
25       -16.73       -19.44        49.36       -63.39        57.02       -32.29
30       -24.10       -27.99        71.07       -91.28        82.10       -46.50
35       -32.80       -38.10        96.74      -124.24       111.75       -63.30
40       -42.84       -49.76       126.35      -162.27       145.96       -82.67
45       -54.22       -62.98       159.91      -205.37       184.73      -104.63
50       -66.93       -77.76       197.42      -253.54       228.06      -129.17