Home > Blockchain >  Homework Help Java and Multidimensional Arrays
Homework Help Java and Multidimensional Arrays

Time:12-03

HW11

Instructions Create an application that contains two arrays. One for the student name and one multi-dimensional array to hold 4 test scores for each student. Have the program calculate the average test score for each student and display the result.

Save the application as StudentScores.java

Upload or copy the java file into the dropbox and submit

public class StudentScores {
   public static void main(String [] args) {
      String [] StudentNames = { "James"   "Cody"   "Thad"   "Zach" };
      double [][] scores = { {1, 84, 95, 67, 78},
                             {2, 96, 100, 87, 92},
                             {3, 90, 85, 56, 77},
                             {4, 83, 87, 92, 74}};

     double avg = 0.0;                     
     double sum = 0.0;                
     
                         
    for(int r = 0; r < scores.length; r  ) {
         System.out.print("Student "   StudentNames   " grades: ");
         for(int c = 1; c < scores[r].length; c  ) {
            System.out.print(scores[r][c]   " ");
            sum = sum   scores[r][c];
         }
         System.out.println();      
      avg = sum / (scores[r].length - 1);
      System.out.println("Average scores: "   avg);
      sum = 0;
         }
        }
       }

This is my current code. The calculator is working my issue is currently not being able to find a method to display the string array in a way that displays the names correctly alongside their grades and averages.

CodePudding user response:

You can use a for loop to iterate over the StudentNames array and print out each student's name along with their grades and average score. To do this, you can use the r variable that is already being used in the for loop to index into the StudentNames array. You can also use the printf method to format the output so that it is easier to read.

Here is an example of how you can modify your code to print out the student names along with their grades and average score:

public class StudentScores {
   public static void main(String [] args) {
      String [] StudentNames = { "James", "Cody", "Thad", "Zach" };
      double [][] scores = { {1, 84, 95, 67, 78},
                         {2, 96, 100, 87, 92},
                         {3, 90, 85, 56, 77},
                         {4, 83, 87, 92, 74}};

     double avg = 0.0;                     
     double sum = 0.0;                
     
    for(int r = 0; r < scores.length; r  ) {
     System.out.printf("Student %s grades: ", StudentNames[r]);
     for(int c = 1; c < scores[r].length; c  ) {
        System.out.print(scores[r][c]   " ");
        sum = sum   scores[r][c];
     }
     System.out.println();      
      avg = sum / (scores[r].length - 1);
      System.out.println("Average scores: "   avg);
      sum = 0;
     }
    }
     }

This will print out the student names along with their grades and average score in the following format:

Student James grades: 84 95 67 78
Average scores: 83.0
Student Cody grades: 96 100 87 92
Average scores: 94.0
Student Thad grades: 90 85 56 77
Average scores: 77.0
Student Zach grades: 83 87 92 74
Average scores: 84.75

CodePudding user response:

I don't have enough "reputation"-points to comment yet, so I'll make an answer instead.

Is the output supposed to look like this:

James: 92
Cody: 84
Thad: 69
Zach: 90

?

  • Related