C 分级程序 : Not Returning 'C' for Grading Program

标签 c

目前我不确定我的程序出了什么问题。我可以获得 A、B、D、F 的正确值;但当成绩在 70 到 80 之间时,它不会返回值 C。

我知道我以一种奇怪的方式绕过了这个程序,这是由于教授设置作业的方式。我能够通过简单的 if 语句获得 C。我认为问题与我的其他(userGrade B)的设置方式有关。

/* Jon Hays
   Assignment 3B Due 9/25/19
   "Grade Calculator"
   This program calculates the average grade (%)
   out of three test scores and converts it to a character
   (A, B, C, D, F)*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main()
{
    double test1;
    double test2;
    double test3;
    char userGrade;

    printf("Please input three test scores:");
    scanf("%lf %lf %lf", &test1, &test2, &test3);

    double testAverage = (test1 + test2 + test3) / 3;
    double second3rdAverage = (test2 + test3) / 2;

    if (testAverage >= 90)
        userGrade = 'A';
    else if (testAverage >= 70 && testAverage < 90)
    {
        if (test3 > 90) 
            userGrade = 'A';
        else 
            userGrade = 'B';
    }
    else if (testAverage >= 50 && testAverage < 70)
    {
        if (second3rdAverage >= 70)
            userGrade = 'C';
        else
            userGrade = 'D';
    }
    if (testAverage <= 50)
        userGrade = 'F';

    printf("%c", userGrade);
}

最佳答案

您的算法是正确的。您在评论中指出的任务

  • If the average score is 90% or more, the grade is A.
  • If the average score is 70% or more and less than 90%, check the third score.
    • If the third score is more than 90%, the grade is A; otherwise the grade is B.
  • If the average score is 50% or more and less than 70%, check the average of the second and third scores.
    • If the average of the two is greater than 70%, the grade is C; otherwise the grade is D.
  • If the average score is less than 50% then the grade is F.

表示,只有当您的总分低于 ​​70% 时才会获得 C。我想,您的假设是手头的任务与实际评分有关 - 但事实并非如此。

关于C 分级程序 : Not Returning 'C' for Grading Program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58088254/

相关文章:

c - 内部操作系统

c - 有没有办法防止 Vscode 在导航代码时切换窗口?

c - c中switch case中的逻辑运算符

c - 为什么我不能在 C 中使用一维数组的输入值?

sql - c & sql - 表不接受插入语句

c - 在 C 中传递一个数组数组

c++ - main() 在 C/C++ 中有多少个参数

c - 我的程序不断失败 - 加载共享库时出错

c - 在C中,如何检查字符串的内容是否不是数字(允许负数)?

c - 简单的 IF Else,但是当我尝试生成错误时命令框崩溃