简体   繁体   中英

Finding the average/highest/lowest grade of input array elements

I'm a beginner Java programmer (like..level 0...). There's this project that I'm working on but I've been stumped for days. I probably also have a lot of little mistakes I didn't notice.

The project is this:

Ask the user to enter a series of grades from 0.00 to 100.00 Do not let the user exceed those boundaries. Make enough space for up to 100 grades. Stop adding grades when the user enters -1. Create a (generic!) function to compute the average of the grades. Also create functions to get the highest, and lowest of the grades. Use these functions to print out and inform the user of the average, highest, and lowest grades.

So far I've got the first half of the code (entering up to 100 grades) but I am completely clueless as to how I should find the average, highest, and lowest grades. Could someone give me ideas or at least give me some sort of example?

Thanks.

Here's my code as of the moment (It's incomplete, especially around the average/highest/lowest grade):

import java.util.*;

 public class GradeStats
 {  
/*
 *Set the array size to 100
 *keep track of where the user is in the array
 *

*/

/*
 * Ask the user to enter a series of grades from 0.00 to 100.00. Do not let the user exceed those boundaries. Make enough space for up to 100 grades.
 * Stop adding grades when the user enters -1. 
 * Create a function to compute the average of the grades. Also create functions to get the highest and lowest of the grades.
 * Use these functions to print out the average, highest, and lowest grades. 
 */

public static void main(String [] args)
{
    // Program loop checker
    boolean done = false;

    // Two doubles used in the equation
    double d1; // Remember we can use commas

    // Scanner to get user input
    Scanner inputReader = new Scanner(System.in);

    // Goal of the program
    System.out.println("\nThis program will find the average, highest, and lowest grades.");    

    // Set up an array of 100 empty slots
    int age[] = new int[100];

    // Program instruction for the user
    System.out.println("Enter a series of grades from 0.00 to 100.00 or type '-1' to exit.\n"); 

    while(!done)
    {
        // Input grade

        double gradeA = inputReader.nextDouble();
        Scanner endReader = new Scanner (System.in); 

        if(gradeA == -1)
        {
                done = true;
        }   
        else if(gradeA > 100)
        {
                done = true;
                System.out.println("Sorry, this is not a valid grade.");
        }   
    }

    int gradeCount; //Number of input grades
    double grade; //Grade Value

    while (gradeCount! = -1)
    {
        gradeCount = gradeCount++
    }

    if (gradeCount !=0)
    {
        double average
        average = double total/ gradeCount;
        System.out.println("The average grade of the students is " +  average(grade));
    }

    // Return the average of an array of integers
      public static double arrayAverage(int intArray[])
      {

        double arrayAverage;
        arrayAverage = gradeA / 3;
        return averageAnswer;
     }
        if (hGrade > lGrade)
        {
        System.out.println("The highest grade is" +hGrade);
        }

        else if (lGrade < hGrade)
        {
            System.out.println("The  grade is +hGrade);
        }

        System.out.println("The lowest grade is" + lGrade);
        System.out.println("The average grade of the students is " +  arrayAverage(grade));
    // Say bye bye
       System.out.println("Bye.");
  }
}

You need to store the grade ( gradeA ) somewhere. The question asks you to compute the average of a set of grades which the user is going to enter, but you've only got just one.

One thing you could do, since you're supposed to have enough space for just 100 grades, is to make an array:

double[] grades = new double[100];

Then while you go through the loop, insert into the array:

int index = 0;
while(!done)
{
    // Input grade

    double gradeA = inputReader.nextDouble();
    Scanner endReader = new Scanner (System.in); 

    if(gradeA == -1)
    {
            done = true;
    }   
    else if(gradeA > 100)
    {
            done = true;
            System.out.println("Sorry, this is not a valid grade.");
    }   
    grades[index] = gradeA;

    index++;
}

Then you can pass that array of integers to your average, min, max functions. (Which I can see you're just getting started on.)

Building off of Denise's answer, you can pass the double array to this function to print the average, min, and max.

private void printAverageMinAndMax(double[] grades){
    double total = 0.0;
    double max = 0.0;
    double min = Double.MAX_VALUE;
    //Loop through all of the grades.
    for(int i = 0; i < 100; i++){
        double grade = grades[i];
        //Add the grade to the total
        total += grade;
        //If this is the highest grade we've encountered, set as the max.
        if(max < grade){
            max = grade;
        }
        //If this is the lowest grade we've encountered, set as min.
        if(min > grade){
            min = grade;
        }
    }

    System.out.println("Average is: " + (total / 100));
    System.out.println("Max is: " + max);
    System.out.println("Min is: " + min);
}

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