gpt4 book ai didi

c - 简单的通过率程序无法正常运行

转载 作者:行者123 更新时间:2023-11-30 16:10:14 25 4
gpt4 key购买 nike

我的教授要求我们构建一个简单的程序,用于接收成绩并显示通过的输入成绩的百分比。

#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");
}

T̶h̶e̶̶p̶r̶o̶b̶l̶e̶m̶̶I̶̶h̶a̶v̶e̶̶i̶d̶e̶n̶t̶i̶f̶i̶e̶d̶̶s̶e̶e̶m̶s̶̶t̶o̶̶b ̶e̶ ̶t̶h̶a̶t̶ ̶"̶P̶a̶s̶s̶i̶n̶g̶"̶i̶s̶n̶'̶t̶ ̶u̶p̶d̶a̶t̶e̶d̶ ̶t̶h̶e̶ ̶f̶i̶r̶s̶t̶ ̶t̶i̶m̶e̶ ̶a̶ ̶g̶r̶a̶d̶e̶ ̶o̶f̶ ̶7̶0̶ ̶o̶r̶ ̶h̶i̶g̶h̶e̶r̶ ̶i̶s̶ ̶i̶n̶p̶u̶t̶.̶ ̶

谢谢大家的帮助。我在循环开始时包含了一个中断操作,但它似乎不起作用。我不明白的那个或另一个问题是掩盖事物。我发现如果我给出 70 -100 的单个输入,则结束循环时收到的输出始终为 0.5。

附注对于错误的格式,我深表歉意,我仍在学习使用该网站。

最佳答案

    if(score >=0  || score <= 100)

每个数字要么大于或等于 0,要么小于或等于 100。此测试没有排除任何数字。

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

关于c - 简单的通过率程序无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com