C if else循环不起作用

时间:2014-03-01 15:01:51

标签: c if-statement for-loop input logic

我不明白为什么if和if else循环不起作用。我也不明白为什么我不能从过去B的程序(不是IRL等级)中获得价值等级。逻辑不会流下来。 所有这个程序的目的是取三个输入等级int(0 -100)并根据“>或<”给出等级已设定。但他们不适合我。我该怎么做才能让if else工作? 感谢。

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

3 个答案:

答案 0 :(得分:1)

您的程序无法编译,因为您的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);
 }

答案 1 :(得分:1)

明确问题陈述。你的程序没有运行因为你正确使用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);
        }
}

但需要检查逻辑。编码错误是这个

答案 2 :(得分:0)

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