2014-03-01 60 views
-1

我不明白爲什麼if和if循環不起作用。我也不明白,爲什麼我無法從過去B的程序(而不是IRL等級)獲得價值等級。邏輯不會流下來。 所有這個程序的目的是做三個輸入等級int(0 -100),並根據設置的「>或<」給出等級。但他們不爲我工作。我能做些什麼來獲得如果其他工作? 謝謝。C如果其他循環不起作用

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

歡迎來到[StackOverflow的](http://stackoverflow.com)。請花一些時間閱讀 [幫助頁面](http://stackoverflow.com/help),尤其是名爲[「我可以問什麼問題?」](http://stackoverflow.com/help /主題)和[「我應該避免問什麼類型的問題?」](http://stackoverflow.com/help/dont-ask)。更重要的是,請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)。您可能還想了解[SSCCE](http://www.sscce.org/)是什麼。 – hivert

+0

顯示的代碼中不存在任何循環。 – alk

回答

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

剛落ifelse

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

讓你的問題陳述清楚。你的程序沒有運行,導致你使用if-else-如果正確。如果你使用其他 - 如果那麼應該有一個條件來檢查。如果沒有條件,那麼你可以使用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); 
     } 
} 

,但你需要檢查的邏輯,即你的程序將運行。在編碼錯誤的這個

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