在Java中打印大型列表数组

在Java中打印大型列表数组,java,arrays,for-loop,count,printf,Java,Arrays,For Loop,Count,Printf,问题是我不知道如何将我的输出输出为以下格式: "Student 1: 98.2" "Student 2: 12.1" "Student 3: 33.6" "Student 4: 47.8" "Student 5: 15.0" "Student 6: 69.2" "Student 7: 55.7" "Student 8: 82.2" ....etc 我必须对宽度使用printf函数,对随机分数使用for循环。不确定如何在printf代码行中列出相邻的学生编号数组。提前感谢您提供的任何提示和帮助

问题是我不知道如何将我的输出输出为以下格式:

"Student 1: 98.2"
"Student 2: 12.1"
"Student 3: 33.6"
"Student 4: 47.8"
"Student 5: 15.0"
"Student 6: 69.2"
"Student 7: 55.7"
"Student 8: 82.2"
....etc
我必须对宽度使用printf函数,对随机分数使用for循环。不确定如何在printf代码行中列出相邻的学生编号数组。提前感谢您提供的任何提示和帮助

***********************下面的程序*************************

import java.util.Scanner;

public class Assignment {
public static void main(String[] args) {

  // input Scanner
  Scanner input = new Scanner(System.in);

  // declare variables
  double[] midtermScores = new double [60];
  double avg = 0.0;
  double sum = 0.0;
  int count = 0;


  // use a for loop to print an array of 60 randomly generated midterm scores
  System.out.println("Here are the Midterm #2 scores for the class:");
  for (int i = 0; i < midtermScores.length; i++) {
     midtermScores[i] = Math.random() * 100;
     System.out.printf("Student %3.1f", midtermScores[i]);
     System.out.println();
     sum += midtermScores[i];
  }

  // find the average of the 60 midterm scores
  avg = sum / midtermScores.length;
  System.out.printf("The average scores is: %3.1f%%", avg);
  System.out.println();

  // count how many students scored above the average
  for (int i = 0; i < midtermScores.length; i++) {
     if (midtermScores[i] > avg) {
        count++;      
     }
  }
  System.out.println(count + " students scored above the average. Good job!");

}
}
Here are the Midterm #2 scores for the class:
Student 151.2
Student 168.8
Student 191.8
Student 195.3
Student 172.9
Student 198.2
Student 141.9
Student 14.1
Student 14.6
Student 151.5
Student 144.0
Student 178.4
Student 130.1
Student 180.8
Student 171.5
Student 158.6
Student 19.4
Student 139.8
Student 15.0
Student 171.5
Student 13.3
Student 197.8
Student 152.2
Student 163.7
Student 111.8
Student 149.2
Student 166.2
Student 113.3
Student 186.0
Student 118.7
Student 16.3
Student 185.0
Student 158.0
Student 142.0
Student 120.9
Student 189.1
Student 149.5
Student 141.3
Student 149.0
Student 18.9
Student 160.7
Student 14.5
Student 130.0
Student 163.3
Student 112.5
Student 117.7
Student 183.8
Student 178.3
Student 111.6
Student 11.1
Student 173.6
Student 151.6
Student 138.1
Student 143.7
Student 190.9
Student 19.4
Student 16.0
Student 136.0
Student 110.1
Student 168.2
The average scores is: 46.2%
31 students scored above the average. Good job!

----jGRASP:操作完成。

如果需要对齐输出,请尝试以下代码:

        System.out.println("Here are the Midterm #2 scores for the class:");
        for (int i = 0; i < midtermScores.length; i++) {
            midtermScores[i] = Math.random() * 100;

            // edited here. %2d to add aligned 2 digit student number
            System.out.printf("\"Student %2d: %3.1f\"", i+1, midtermScores[i]);
            System.out.println();
            sum += midtermScores[i];
        }

如果您不想要
(引号),请从
printf中删除

您到底面临什么问题?如何打印学生编号?看看打印这些行的循环-是否有任何内容会随着每次迭代而增加,并且可能代表一个学生编号?提示:它是以0为基础的,因此您可能必须添加1才能获得1、2、3等数字。对于浮点数,宽度应为5,因为它包含包括小数点在内的整个数字。Mahalo!它起作用了,但我想知道为什么以前我写代码时它对我不起作用,就像。。。。。。System.out.printf(“学生%3.1f”,i+1,期中成绩[i])?但当我在“%2d”中添加时,它将运行程序,不会出现错误。
Here are the Midterm #2 scores for the class:
"Student  1: 85.7"
"Student  2: 5.9"
"Student  3: 74.7"
"Student  4: 0.5"
"Student  5: 90.8"
"Student  6: 76.7"
"Student  7: 64.0"
"Student  8: 34.7"
"Student  9: 54.9"
"Student 10: 1.3"
"Student 11: 19.9"
"Student 12: 96.5"
"Student 13: 53.0"
"Student 14: 99.9"
...