我遇到显示最低平均值的问题。我已经终止了考试成绩输入的终止价值。当我触发-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;
}
答案 0 :(得分:1)
修复while循环的逻辑。您在检查minScore
是否为-999之前更新examScore
,这样做不好。
此外,您应该在使用之前初始化examScore
之类的变量,并且不应该使用浮点数对examScore != -999
进行精确比较。要么更改为整数,要么进行更宽容的比较,例如examScore < -998
。
答案 1 :(得分:1)
考虑以下计划。会do_stuff
被调用吗?
int foo;
while (foo != 5) {
foo = 5;
do_stuff();
}
实际上行为是未定义的,因为foo
在初始化之前已经过测试。您的代码与examScore
具有相同的问题。由于这是作业,这只是一个提示 - 你如何修复上面的简单程序?
答案 2 :(得分:0)
该代码的问题在于,当用户输入-999时,它会将其作为测试分数进行处理。循环需要重新构建,以便在用户输入后立即中断。
答案 3 :(得分:0)
与其他人一样,您在处理完该分数后会对-999
进行检查。另外我假设你应该只在当前考试较低时更新minScore
所以你应该有类似的东西:
if(maxScore < examScore)
{
maxScore = examScore;
}
if(minScore > examScore)
{
minScore = examScore;
}
最后,您应该在使用之前初始化所有变量。
编辑:
while(...)
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);
if(examScore < 0 || examScore > 100)
{
printf("Exam Scores range from 0.0 - 100.0\n");
continue; //goes back to the top of the loop skipping everything else
}
//At this point you know the score is valid so you can process it
scoreCount++;
if(maxScore < examScore)
{
maxScore = examScore;
}
if(minScore > examScore)
{
minScore = examScore;
}
sumScore += examScore;
avgScore = sumScore / scoreCount;
}
如果它不在循环中或您不想使用continue
,您可以在有效性检查后将其他所有内容(所有处理内容)放在else
块中。
答案 4 :(得分:0)
除了没有init
的值之外还有一个逻辑错误,并且直接比较拖曳浮点问题。
在你的情况下1 while循环,你需要确定用户输入是否首先满足终止条件,然后处理它。另外minScore
需要在分配之前进行比较,就像twain249所说的那样。
答案 5 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int scoreCount = 0;
float maxScore = -1000.0,
minScore = 1000.0,
sumScore = 0.0;
printf("************************************\n");
printf("1-> Enter Exam Scores\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");
for(;;)
{
int menuChoice;
printf("Select a number that corresponds to the menu:\n");
scanf("%d", &menuChoice);
if(scoreCount == 0 && menuChoice >= 2 && menuChoice <= 4)
{
printf("Please enter some scores by choosing menu item 1\n");
continue;
}
switch(menuChoice)
{
case 1:
for(;;)
{
float examScore;
printf("Enter an exam score (enter -999 to quit score input):\n");
scanf("%f", &examScore);
if (examScore == -999)
break;
printf("The score you've entered equates to : %.2f\n\n", examScore);
if(examScore < 0 || examScore > 100)
{
printf("Exam scores range from 0.0 - 100.0\n");
continue;
}
sumScore += examScore;
++scoreCount;
if(maxScore < examScore)
{
maxScore = examScore;
}
if(minScore > examScore)
{
minScore = examScore;
}
}
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:
{
float avgScore = sumScore / scoreCount;
printf("The average exam score is : %.2f\n", avgScore);
break;
}
default:
printf("You've entered an invalid menu choice, review more carefully!");
break;
}
}
return 0;
}
答案 6 :(得分:0)
有两个问题:负分数存储为最小值,但是当修正时,最小分数的初始值将为0,因此您无法获得更高的值。 如果min-check的输入不超过100(代码注释中为initA)或初始值为第一个值(基于分数计数 - initB),则将最小分数初始化为100。 输入考试分数也可以通过简单地提供超出范围的值来退出:
float minScore = 100.0; /* initA */
/* .. */
case 1 :
{
printf("Enter an exam score (0..100): "); scanf("%f", &examScore);
while( ( 0.0 <= examScore ) && ( examScore <= 100.0 ) )
{
if( 0 == scoreCount ) { minScore = examScore; } /* initB */
++scoreCount;
if( maxScore < examScore ) { maxScore = examScore; }
if( minScore < examScore ) { minScore = examScore; }
sumScore += examScore;
avgScore = sumScore / scoreCount;
printf("Enter an exam score (0..100): "); scanf("%f", &examScore);
}
break;
}
并非所有printf都以\n
关闭。