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;
}
}
Приветствую всех! Прямо сейчас у меня проблема с моим кодом, и я не могу понять, почему. Код должен отправить обратно количество баллов, которые превышают или равны среднему значению, и числа, которые ниже среднего.Почему моя программа не возвращает правильный номер?
Иногда код не возвращает правильный номер. Например, если я набираю 3 теста, которые составляют 100, 90 и 80, среднее значение равно 90. Оно должно показать, что 2 выше/равно 90 и 1, что ниже 90. Проблема заключается в том, что она показывает, что 0 ниже 90. Я тестировал его несколько раз, и, похоже, это происходит только ниже среднего. Заранее спасибо!
Я думаю, что это потому, что вы начинаете с 'индексом = 1' начинаются с' индексом = 0' – TheUknown