繁体   English   中英

C,否则else循环不起作用

[英]C if else loops don't work

我不明白为什么if和if else循环不起作用。 我也不明白为什么我不能从程序中获得超过B的价值等级(不是IRL等级)。 该程序要做的全部工作是取三个输入等级int(0 -100),然后根据设置的“>或<”给出等级。 但是他们对我不起作用。 我该怎么做才能使其他功能正常工作? 谢谢。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

// Functions 
void readScores(int* test1, int* test2, int* test3);
int determineAverage(int test1, int test2, int test3);
void print(int test3, int average, int test2_test3Av);
int avgtest2test3(int test2, int test3);


int main(void)
{
    int test1;
    int test2;
    int test3;
    int average;
    int test2_test3Av;

    readScores(&test1, &test2, &test3);  // takes input
    average = determineAverage(test1, test2, test3); // finds average
    test2_test3Av = avgtest2test3(test2, test3); // gets average of test one and two
    print(test3, average, test2_test3Av); // Prints grade resluts
    return 0;
}

void readScores(int *test1, int *test2, int *test3)
{
    // Promts
    printf("\nHello, this program will determine");
    printf("\nthe grades of an average test scores");
    printf("\nto see if you passed or not this year.");
    printf("\nPlease enter in the three test...");
    printf("\nNote: only enter scores that are (0-100)\n");

    printf("Enter in test 1\n");
    scanf(" %d", test1);
    printf("Enter in test 2\n");
    scanf(" %d", test2);
    printf("Enter in test 3\n");
    scanf(" %d", test3);
    return;
}
int determineAverage(int test1, int test2, int test3)
{
    // Local declrations
    int average;

    // Math
    average = (test1 + test2 + test3) / 3;
    return average;
}
void print(int test3, int average, int test2_test3Av)
{
    if (average >= 90)
    {
        printf("Great job you have an A %d int the class\n", average);
    }
    else if (average >= 70 && average <= 90, test3)
    {
        if (test3 >= 90)
        {
            printf("Good job you got a A %d\n", average);
        }
        else if
        {
            printf("Easy Beezy you got a B %d for the class\n", average);
        }
    }
    else if (average >= 50 && average <= 70)
    {

        if (test2_test3Av >= 70)
        {
            printf("You passed congrats you have a C %d for the class\n", average);
        }
        else if
        {
            printf("You have a D for the class %d\n", average);
        }
    }
    else if (average <= 50)
    {
        printf("Yeah you might want to take this class again you have a F %d\n", average);
    }
    return;
}

int avgtest2test3(int test2, int test3)
{
    int holder;
    holder = ((test2 + test3) / 2);
    return holder;
}

您的程序无法编译,因为您的else语句错误:

 if (test3 >= 90)
 {
   printf("Good job you got a A %d\n", average);
 }
 else if // << here is the problem
 {
   printf("Easy Beezy you got a B %d for the class\n", average);
 }

只需将if放在else后面:

 if (test3 >= 90)
 {
   printf("Good job you got a A %d\n", average);
 }
 else
 {
   printf("Easy Beezy you got a B %d for the class\n", average);
 }

使您的问题陈述清楚。 您的程序未运行,原因是您不能正确使用if-else-if。 如果使用else-if,则应该有一个检查条件。 如果没有条件,则可以使用if-else。 即如果您进行更改,您的程序将运行

if (test3 >= 90)
        {
            printf("Good job you got a A %d\n", average);
        }
        else if
        {
            printf("Easy Beezy you got a B %d for the class\n", average);
        }
    }
    else if (average >= 50 && average <= 70)
    {

        if (test2_test3Av >= 70)
        {
            printf("You passed congrats you have a C %d for the class\n", average);
        }
        else if
        {
            printf("You have a D for the class %d\n", average);
        }

此代码部分

if (test3 >= 90)
        {
            printf("Good job you got a A %d\n", average);
        }
        else
        {
            printf("Easy Beezy you got a B %d for the class\n", average);
        }
    }
    else if (average >= 50 && average <= 70)
    {

        if (test2_test3Av >= 70)
        {
            printf("You passed congrats you have a C %d for the class\n", average);
        }
        else
        {
            printf("You have a D for the class %d\n", average);
        }
}

但是您需要检查逻辑。 编码错误是这样的

else if (average >= 50 && average <= 70)
{

}

if (test3 >= 90)
 {
   printf("Good job you got a A %d\n", average);
 }
 else
 {
   printf("Easy Beezy you got a B %d for the class\n", average);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM