简体   繁体   中英

display grade lowest and highest

How do I display the highest and lowest average grade? I tried other solutions but it's not working

Can you help how to display the highest average grade and the lowest average

public static void main(String[] args)
{
    Scanner console = new Scanner (System.in);
    int numStud, numGrade = 0, count = 0, count2 = 0;

    double totalgrades = 0;

    System.out.println("Enter number of students:");
    numStud = console.nextInt();
    System.out.println("Enter number of student grades:");
    numGrade = console.nextInt();
    String[] students = new String [numStud];
    double [][] grades = new double [numStud][numGrade];
    double[] average = new double[numStud];
    String x = console.nextLine();

    System.out.println("");
    while(count<numStud)
    {
        System.out.println("Enter name of student " + (count + 1) + ":");
        students[count] = console.nextLine();
        System.out.println("Enter " + numGrade + " grades:");
        while(count2<numGrade)
        {
            grades[count][count2] = console.nextDouble();
            totalgrades = totalgrades + grades [count][count2];
            count2++;

        }
        average[count] = totalgrades / numGrade;
        totalgrades = 0;
        String y = console.nextLine();
        count2 = 0;
        count++;



}
count = 0;
count2 = 0;
while(count<numStud)
{
    System.out.println("The grades of " + students[count] + " are:");
    while(count2<numGrade)
    {
        System.out.println(grades[count][count2] + " ");
        count2++;
    }
    System.out.println("The average is " + average[count] + ".");
    System.out.println("");
    count2 = 0;
    count++;        
}

Suppose all average grades keeps in average array. Then,

Arrays.sort(average);
System.out.print("min = " + average[0] + ", max = " + average[average.length - 1]);

To fill average you have to:

for(int j = 0; j < grades.length; ++j) {
    int sum = 0;
    for(int i = 0; i < grades[j].length; ++i) sum += grades[j][i];

    average[j] = sum / numGrade;
}

Filling grades array you have done correctly. But I advise you to turn the while loop into the for loop.

To find the highest :

  • Set a variable say max as the first element (or 0)
  • Loop though the entire array
  • Compare the value of each array element against max
  • If element value > max
  • Update max with element's value
  • At end of loop you will get maximum value of the array.

     int max = Integer.MIN_VALUE; for(int x=0; x<average.length; x++) if(average[x] > max) max = average[x];

To find the lowest :

  • Set a variable say min as the first element (or 0)
  • Loop though the entire array
  • Compare the value of each array element against min
  • If element value < min
  • Update min with element's value
  • At end of loop you will get minimum value of the array.

     int min = Integer.MAX_VALUE; for(int x=0; x<average.length; x++) if(average[x] < min) min = average[x];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM