2015-02-05 7 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)' Я не вижу массив OR ArrayList в коде, который вы указали. – csmckelvey

+0

Вы также должны добавить, какая у вас проблема, и что вы ожидаете. – jnd

+0

rightnow Я пытаюсь выяснить, как сохранить оценки в arraylist –

ответ

0

Добавьте отдельные оценки в цикле:

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

вы можете сделать еще один цикл для отображения ваших классов в обратном направлении:

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

Надеется, что это помогает.

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); 
    } 
Смежные вопросы