查找输入数组元素的平均/最高/最低级别



我是一名初级Java程序员(比如..level 0…)。我正在做这个项目,但我已经被难住了好几天。我可能也有很多我没有注意到的小错误。

项目是这样的:

要求用户输入从0.00到100.00的一系列分数。不要让用户超过这些界限。留出足够的空间,最多可容纳100个等级。当用户输入-1时,停止添加成绩。创建一个(泛型!)函数来计算成绩的平均值。还可以创建函数来获得最高和最低级别。使用这些功能打印并通知用户平均、最高和最低等级。

到目前为止,我已经掌握了代码的前半部分(最多输入100个等级),但我完全不知道如何找到平均、最高和最低等级。有人能给我一些想法吗,或者至少给我一些例子吗?

谢谢。

这是我目前的代码(它不完整,尤其是在平均/最高/最低级别附近):

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.");
  }
}

您需要将等级(gradeA)存储在某个地方。这个问题要求你计算用户将要输入的一组成绩的平均值,但你只有一个。

你可以做的一件事是,因为你应该有足够的空间来容纳100个等级,那就是制作一个数组:

double[] grades = new double[100];

然后,当你遍历循环时,插入数组:

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

然后,您可以将整数数组传递给平均值、最小值、最大值函数。(我可以看出你才刚刚开始。)

根据Denise的答案,您可以将双数组传递给该函数,以打印平均值、最小值和最大值

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

相关内容

  • 没有找到相关文章

最新更新