最低平均の表示に問題があります。試験のスコア入力を終了する終了値を作成しました。-999 をトリガーして最低の試験スコアを表示すると、実際の最低スコアではなく -999 の値が表示されます。この値を除外するにはどうすればよいですか?
これが私のコードです:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int menuChoice, scoreCount = 0;
float examScore = 0.0,
maxScore = 0.0,
minScore = 0.0,
avgScore = 0.0,
sumScore = 0.0;
printf("************************************\n");
printf("1-> Enter an Exam Score\n");
printf("2-> Display the highest exam score\n");
printf("3-> Display the lowest exam score\n");
printf("4-> Display the average exam score\n");
printf("5-> Quit the program\n");
printf("************************************\n\n");
printf("Select a number that corresponds to the menu :\n");
scanf("%d", &menuChoice);
while(menuChoice != 5)
{
switch(menuChoice)
{
case 1:
while(examScore != -999)
{
printf("Enter an exam score (enter -999 to quit score input):\n");
scanf("%f", &examScore);
printf("The score you've entered equates to : %.2f\n\n", examScore);
scoreCount++;
if(maxScore < examScore)
{
maxScore = examScore;
}
if(minScore > examScore)
{
minScore = examScore;
}
else if(examScore < 0 || examScore > 100)
{
printf("Exam Scores range from 0.0 - 100.0\n");
}
sumScore += examScore;
avgScore = sumScore / scoreCount;
}
break;
case 2:
printf("The highest exam score is : %.2f\n", maxScore);
break;
case 3:
printf("The lowest exam score is : %.2f\n", minScore);
break;
case 4:
printf("The average exam score is : %.2f", avgScore);
break;
default:
printf("You've entered an invalid menu choice, review more carefully!");
break;
}
printf("Select a number that corresponds to the menu :\n");
scanf("%d", &menuChoice);
}
system("pause");
return 0;
}