Home > OS >  Display total and average in array
Display total and average in array

Time:10-10

Hello I'm trying to display the total scores of each class and the average of each student. It always fails to show up. I've been able to display the names and scores. They turn up perfectly, but i cant get the average and total. Please what do I need to do to make it appear. I want the Average to be on the big circle beside class 3, then the total under Tina. How do I do that? enter image description here

import java.util.*;

public class Quiz {
public static void main(String[] args) {
    //String array to hold first names
    String firstName[] = new String[9];

    //double array to hold average
    double average[] = new double[5];
    double total[] = new double[5];
    //double 2 dimensional array to hold grades
    double[][] studentGrades = new double[5][3];
    //variable to hold over seventy
    int overSeventy = 0;

    //scanner object
    Scanner in = new Scanner(System.in);

    //for loop to create array of first name and last name
    for(int i = 0; i < 5; i  )
    {
        //prompt user
        System.out.print("Enter the first name: ");
        firstName[i] = in.nextLine();//user input
    }//for

    //Display students names
    //print underline
    System.out.println("----------------------");
    //for loop to display firstname and last name form arrays
    for(int i = 0; i < 5; i  )
    {
        // print names
        System.out.println(firstName[i]);
    }//for

    System.out.println();// blank lines

    //store student grades
    // first for loop to create rows
    for(int row = 0; row < studentGrades.length; row  )
    {
        //second for loop to create columns
        for(int column = 0; column < studentGrades[row].length; column  )
        {
            //prompt the user to enter grades
            System.out.println("Enter grade for each course for "   firstName[row]);
            //Prompting to enter course grade
            System.out.println("Course"   (column 1)   ": ");
            studentGrades[row][column] = in.nextDouble();//user input
        }//end column for
    }//end row for

    //find the average
    for(int i = 0; i < 5; i  )
    {
        average[i] = (studentGrades[i][0]   studentGrades[i][1]  
                studentGrades[i][2]) / 3;
    }//for




    //Print Table
    System.out.println("                        Student Grades                   ");
    System.out.println("Name"   "\t"   "\t"   "Class1"   "\t"   "\t"   "Class2"   "\t"   "\t"   "Class3");
    System.out.println("----------------------------------------------------------------------------");

    //print student names and test scores
    for(int row = 0; row < studentGrades.length; row  )
    {
        System.out.print(firstName[row]   "\t"   "\t");
        //for loop to print names and grades
        for(int column = 0; column < studentGrades[row].length; column  )
        {
            total[column] = (studentGrades[row][0]   studentGrades[row][1]   studentGrades[row][2]);
            System.out.print(studentGrades[row][column]   "\t"   "\t");

        }
        System.out.println();
    }//end for


    System.out.println();// Blank lines


}
}

CodePudding user response:

you need to calculate total total = num and before calculate average = total/3

Total

(in column loop)

total[row]  = studentGrades[row][column];

Average

(in row loop)

average[row] = total[row]/3;
System.out.print(average[row]   "\t"   "\t");
System.out.print(total[row]   "\t"   "\t");

Output

enter image description here

Full Code


import java.util.*;

public class Quiz {
        public static void main(String[] args) {
            //String array to hold first names
            String firstName[] = new String[9];

            //double array to hold average
            double average[] = new double[5];
            double total[] = new double[5];
            //double 2 dimensional array to hold grades
            double[][] studentGrades = new double[5][3];
            //variable to hold over seventy
            int overSeventy = 0;

            //scanner object
            Scanner in = new Scanner(System.in);

            //for loop to create array of first name and last name
            for(int i = 0; i < 5; i  )
            {
                //prompt user
                System.out.print("Enter the first name: ");
                firstName[i] = in.nextLine();//user input
            }//for

            //Display students names
            //print underline
            System.out.println("----------------------");
            //for loop to display firstname and last name form arrays
            for(int i = 0; i < 5; i  )
            {
                // print names
                System.out.println(firstName[i]);
            }//for

            System.out.println();// blank lines

            //store student grades
            // first for loop to create rows
            for(int row = 0; row < studentGrades.length; row  )
            {
                //second for loop to create columns
                for(int column = 0; column < studentGrades[row].length; column  )
                {
                    //prompt the user to enter grades
                    System.out.println("Enter grade for each course for "   firstName[row]);
                    //Prompting to enter course grade
                    System.out.println("Course"   (column 1)   ": ");
                    studentGrades[row][column] = in.nextDouble();//user input
                }//end column for
            }//end row for

            //find the average
            for(int i = 0; i < 5; i  )
            {
                average[i] = (studentGrades[i][0]   studentGrades[i][1]  
                        studentGrades[i][2]) / 3;
            }//for




            //Print Table
            System.out.println("                        Student Grades                   ");
            System.out.println("Name"   "\t"   "\t"   "Class1"   "\t"   "\t"   "Class2"   "\t"   "\t"   "Class3"  "\t"   "\t"   "Average"   "\t"   "\t"   "Total");
            System.out.println("----------------------------------------------------------------------------");

            //print student names and test scores
            for(int row = 0; row < studentGrades.length; row  )
            {
                System.out.print(firstName[row]   "\t"   "\t");
                //for loop to print names and grades
                for(int column = 0; column < studentGrades[row].length; column  )
                {
                    total[row]  = studentGrades[row][column];
                    System.out.print(studentGrades[row][column]   "\t"   "\t"  "\t");
                }
                average[row] = total[row]/3;
                System.out.print(average[row]   "\t"   "\t");
                System.out.print(total[row]   "\t"   "\t");
                System.out.println();
            }//end for


            System.out.println();// Blank lines


        }
}
  •  Tags:  
  • java
  • Related