程序要求“编写一个Java程序,该程序将首先询问用户他们想要输入多少个等级。然后使用do..while循环用用户输入的等级填充该大小的数组。然后进行排序数组。在for循环中读取该数组,显示成绩并总计成绩。循环后,计算这些成绩的平均值并显示该平均值。
输出是问题。无论我对代码做什么,它只会输出我键入的两个输入。如果我选择输入4,5或10个成绩。它只会显示最低的两个。虽然总数和平均值是正确的。我在这里想念什么?
这是我写的:
import java.util.Scanner;
import java.util.Arrays;
public class TapCoGradeArray
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int count;
double inputGrade = 0;
double gradeTotal = 0;
double[] individualGrade;
System.out.println("Enter the number of students that are being graded.");
int numberOfGrades = keyboard.nextInt();
individualGrade = new double[numberOfGrades];
count = 0;
do
{
System.out.println("Enter the grade (from 0-100) for each student below.");
inputGrade = keyboard.nextDouble();
individualGrade[count] = inputGrade;
count++;
gradeTotal+= inputGrade;
} while(count < numberOfGrades);
Arrays.sort(individualGrade);
for(count = 0; count == individualGrade.length; count++);
{
// This next line is using gradeTotal as an array. However, there is no array by that name.
// Check which array this should be.
System.out.println("The grades entered are the following: \n" + inputGrade + "\n" + individualGrade[count]);
}
double gradeAverage = gradeTotal / numberOfGrades;
System.out.println("The total of the grades is " + gradeTotal);
System.out.println("The average of the grades entered is " + gradeAverage);
}
}
答案 0 :(得分:4)
两个主要问题
for(count = 0; count == individualGrade.length; count++);
在达到此值之前,此字符不应该包含seimcolon和循环。
for(count = 0; count < individualGrade.length; count++)
{
System.out.println("The grades entered are the following: \n" +
inputGrade + "\n" + individualGrade[count]);
}