2014-12-29 85 views
2

我必須打印由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 
+0

你能提供一個樣本輸出?您也可以使用String構建器以指定的格式添加每行,並以換行符結尾,這樣可以保證格式。 – holtc

+0

我發表了輸出 – apsstudent

回答

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 

你的問題是從\nSystem.out.printf("%5d\n", MPH[row]);

+0

噢,我的天啊,謝謝。 – apsstudent

+0

@apsstudent歡迎您:) – Lrrr

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(); 
    } 
} 

出把

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 
+0

謝謝!完美 – apsstudent

+0

@apsstudent np! – Kalenda