完全通过程序的百分比无法正常运行

问题描述 投票:0回答:1

[我的教授请我们建立一个简单的程序,该程序可以录入成绩并显示通过的输入成绩的百分比。

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int score=0;
    double percentPassing = 0, sum = 0, passing = 0;


    do
    {
        printf("Enter a test score (-1 to quit):");
        scanf("%i", &score);

        if(score == -1){break;}

        if (score > 100)
        {
            printf("That is not a valid grade!");
        }
        if(score >=0  || score <= 100)
        {
            sum = sum + 1;
        }
        if(score >= 70&&score <=100)
        {
            passing = passing + 1;

        }
        percentPassing = (passing/sum);
    }
    while (score != -1)


printf("\n Percent of grades that are passing: %.2d\n", percentPassing);
system("pause");
}

问题我已经确定似乎是̶“通过”̶̶i̶s̶n̶'̶t̶更新的第一时间等级为70或更高̶i̶n̶p̶u̶t̶.̶̶

谢谢大家的帮助。我在循环开始时包含了一个break操作,但并没有奏效。我不明白的那个或另一个问题是掩盖事物。我发现如果给定70 -100的单个输入,则结束循环时收到的输出始终为.5。

P.S。我为仍在使用该网站的格式错误而道歉。

c
1个回答
0
投票
    if(score >=0  || score <= 100)

每个数字都大于或等于零或小于或等于100。此测试没有排除任何数字。

为了防止-1计数,您可以在其他if (score == -1) break;测试之前添加if。另外,您可能不希望得分为120导致passing递增。

© www.soinside.com 2019 - 2024. All rights reserved.