2

我在显示最低平均值时遇到问题。我已经设定了结束考试分数输入的终止值。当我触发 -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;
}
4

7 回答 7

1

修复 while 循环的逻辑。minScore您在检查是否为 -999 之前更新examScore,所以这不好。

此外,您应该在使用它们之前初始化变量examScore,并且不应该像examScore != -999使用浮点数那样进行精确比较。要么更改为 ints,要么进行更宽容的比较,例如examScore < -998.

于 2012-05-29T22:49:52.437 回答
1

考虑以下程序。会do_stuff叫吗?

int foo;
while (foo != 5) {
   foo = 5;
   do_stuff();
}

实际上,行为是未定义的,因为foo在初始化之前已经过测试。您的代码有同样的问题,使用examScore. 由于这是家庭作业,这只是一个提示——你将如何修复上面的简单程序?

于 2012-05-29T22:52:40.587 回答
0

该代码的问题在于,当用户输入 -999 时,它会将其处理为测试分数。循环需要重组,以便在用户输入后立即中断。

于 2012-05-29T22:52:29.780 回答
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

于 2012-05-29T22:54:51.443 回答
0

除了没有init直接比较两个浮点问题的值之外,还有一个逻辑错误。在您的情况 1 while 循环中,您需要先确定用户输入是否满足终止条件,然后再对其进行处理。还minScore需要在分配之前进行比较,就像 twain249 所说的那样。

于 2012-05-30T02:44:05.413 回答
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;
}
于 2012-06-02T03:10:14.003 回答
0

有两个问题:负分数存储为最小值,但当固定时,最小值分数的初始值将为 0,因此您无法获得更高的值。如果 min-check 的输入不超过 100(代码注释中的 initA)或使用第一个值初始化(基于分数计数 - initB),则将 min score 初始化为 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.

于 2015-09-24T08:26:23.027 回答