Printing a large listed array in Java

DMJ :

The problem is I can't figure out, how to have my output come out as below format:

"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

I have to use the printf function for width and the for loop for the random scores. Not sure how to list this array of student numbers next to each other in the printf line of code. Thank you in advance for any tips and help!

*********************** PROGRAM BELOW *************************

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

}
}

************************** OUTPUT BELOW *******************************

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: operation complete.

ruhul :

If your need to align your output try this code:

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

So the output will be:

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"
...

Remove \" from printf if you don't want "(quotes).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=172150&siteId=1