2015-02-05 52 views
-3

我需要做的是創建一個存儲所有分數並反向顯示數字的數組。幫助~~ !! 這是迄今爲止我想出的。java arraylist的問題

// wbin

import java.util.Scanner; 
    import java.text.DecimalFormat; 

    public Class ArrayPractice 
    { 
    public static void main(String[] args) { 
    int count = 0; 
    int total = 0; 
    final int SENTINEL = 0; 
    int score; 
    int sum; 


    Scanner scan = new Scanner(System.in); 

    System.out.println("Enter your score then Press 0 to display your average and total score. "); 
    System.out.println("When you are finished, Press 0"); 

    System.out.print("Enter the first test score > "); 
    score = scan.nextInt(); 

    while (score != SENTINEL) 
    { 
     total = score + total; 

     count++; 

     System.out.print("Enter the next test score > "); 
     score = scan.nextInt(); 
    } 



    if (count != 0) 
    { 
     DecimalFormat oneDecimalPlace = new DecimalFormat("##.0"); 
     System.out.println("\nYour Average is " 
      + oneDecimalPlace.format((double) (total/count))); 
      System.out.println("Your Total score is " + total); 
} 
     else 
     System.out.println("\nNo grades were entered"); 

    } 
} 
+0

'1)方法'ArrayList的是不一樣的東西數組,你似乎當你談論他們能夠相互交換2。 '2)'我在你提供的代碼中看不到數組或ArrayList。 – csmckelvey

+0

您還需要添加您的問題以及您的期望。 – jnd

+0

rightnow我想弄清楚如何將成績保存到arraylist –

回答

0

添加在你的循環中的個人得分:

for(int i = scores.size(); i > 0; i--){ 
    System.out.println(scores.get(i)); 
} 

希望這:

ArrayList<Integer> scores = new ArrayList<Integer>(); 
while (score != SENTINEL) 
{ 
    scores.add(score) 
    total = score + total; 

    count++; 

    System.out.print("Enter the next test score > "); 
    score = scan.nextInt(); 
} 

您可以反向顯示你的成績做另一個循環幫助。

0

另一個打印項目以相反的順序如下

Scanner scan = new Scanner(System.in); 
    List<Integer> scores = new ArrayList<Integer>(); 
    int totalScore =0; 
    while(true) { 
     System.out.print("Enter the test score > "); 
     int score=scan.nextInt(); 
     if(score==0) break; 
     totalScore+=score; 
     scores.add(score); 
    } 

    Collections.reverse(scores); 

    for(int score:scores){ 
     System.out.println(score); 
    }