2015-11-15 55 views
0

系統:麻煩會議規範格式錯誤

.out.println("For test 1,"); 
    for (int i = 0; i < testOne.length; i++) 
    { 
     System.out.print("Enter score " + (i + 1) + ": "); 
     testOne[i] = keyboard.nextInt(); 
    } 
    System.out.println(); 

    System.out.println("For test 2,"); 
    for (int i = 0; i < testTwo.length; i++) 
    { 
     System.out.print("Enter score " + (i + 1) + ": "); 
     testTwo[i] = keyboard.nextInt(); 
    } 
    for (int i = 0; i < average.length; i++) 
    { 
     average[i] = ((testOne[i] + testTwo[i])/2.0D); 
     if (Double.doubleToLongBits(average[i]) > Double.doubleToLongBits(90.0D)) { 
     grade[i] = 'A'; 
     } else if (Double.doubleToLongBits(average[i]) > Double.doubleToLongBits(80.0D)) { 
     grade[i] = 'B'; 
     } else if (Double.doubleToLongBits(average[i]) > Double.doubleToLongBits(70.0D)) { 
     grade[i] = 'C'; 
     } else { 
     grade[i] = 'F'; 
     } 
    } 
    System.out.println(); 
    System.out.println("Test 1\tTest 2\tAverage\tGrade"); 
    System.out.println("------\t------\t-------\t-----"); 
    for (int i = 0; i < testOne.length; i++) { 
     System.out.println(testFormat.format(testOne[i]) + "\t" + testFormat.format(testTwo[i]) + "\t" + averageFormat.format(average[i]) + "\t" + grade[i]); 
    } 
    } 
} 

The output looks like this: 
Test 1 Test 2 Average Grade 
------ ------ ------- ----- 
089 098 093.5 A 
097 085 091.0 A 
094 096 095.0 A 
078 073 075.5 C 

What I need: 
Test 1 Test 2 Average Grade 
------ ------ ------- ----- 
089 098  093.5 A 
097 085  091.0 A 
094 096  095.0 A 
078 073  075.5 C 

我想知道是,如果有人可以幫助我的,阻止我未能完成該程序的格式問題?我需要將這些數據歸入正確的列和類別,因爲這是規範要求的。對簡單格式問題的任何幫助都會很好,深受讚賞。

回答

0

您可以用填充工作:

String columnName = "Test 1"; 
int columnLength = column.length; 
System.out.println(String.format("%1$-" + columnLength + "s", value)); 

這給你的列標題大小的右填充。如果這些值是你期望的類型應該可以正常工作。如果你的行值變大了,你必須小心如果寬度高於標題寬度。在這種情況下,您可能還想增加標題之間的空白。

0

你需要testFormat.format()averageFormat.format()

刪除它們後:

for (int i = 0; i < testOne.length; i++) { 
    System.out.println(testOne[i] + "\t" + testTwo[i] + "\t" + average[i] + "\t" + grade[i]); 
} 

這是輸出:

Test 1 Test 2 Average Grade 
------ ------ ------- ----- 
90  56  73.0 C 
95  98  96.5 A 
90  98  94.0 A 
97  99  98.0 A