簡體   English   中英

查找輸入數組元素的平均/最高/最低等級

[英]Finding the average/highest/lowest grade of input array elements

我是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的答案,您可以將double數組傳遞給此函數以打印平均值,最小值和最大值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM