我是一名初学者Java程序员(如...级别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.");
}
}
答案 0 :(得分:1)
您需要在某处存储成绩(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++;
}
然后你可以将整数数组传递给你的平均,最小,最大函数。 (我可以看到你刚刚开始。)
答案 1 :(得分:0)
根据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);
}