【发布时间】:2015-05-09 11:26:25
【问题描述】:
import java.util.Scanner;
public class AnalyzingScores
{
public static void main(String[] args)
{
int count = 0;
double scoreTotal = 0;
int index;
int tests;
double scoreAverage;
double highest;
double lowest;
//Creates a new Scanner.
Scanner keyboard = new Scanner(System.in);
//Asks the user to enter how many tests they have taken.
System.out.println("Enter the amount of tests you have taken: ");
tests = keyboard.nextInt();
//Creates an array.
int[] score = new int[tests];
//Creates a for loop that asks a user to enter their test scores.
for (index = 0; index < tests; index++)
{
System.out.println("Enter your test score: ");
score[index] = keyboard.nextInt();
scoreTotal += score[index];
}
scoreAverage = scoreTotal / score.length;
System.out.println("You entered " + tests + " scores.");
System.out.println("The test average is " + scoreAverage);
System.out.println("Number of scores above or equal to the average is " + getHighest(score));
System.out.println("Number of scores below the average is " + getLowest(score));
}
private static int scoreAverage;
public static int getHighest(int[] score)
{
int aboveAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score)
{
int belowAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
}
大家好!现在我的代码有问题,我不知道为什么。该代码应该发送回高于或等于平均数和低于平均数的分数。
但有时,代码不会返回正确的数字。例如,如果我输入 100、90 和 80 的 3 个测试分数,则平均值为 90。它应该显示 2 高于/等于 90 和 1 低于 90。问题是,它显示 0低于 90。我已经对其进行了多次测试,但似乎只发生在低于平均水平。提前致谢!
【问题讨论】:
-
我认为那是因为您以
index=1开头以index=0开头