有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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" );

  }
}

共 (1) 个答案

  1. # 1 楼答案

    在循环中添加个人分数:

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

    希望这有帮助