我想弄清楚我的checkQuestion
函數有什麼問題。我不知道爲什麼,但有時它不會檢查某些問題或完全跳過它們。我創建了一個函數createQuestion
,我在createQuestion
中調用了checkQuestion
,所以我不知道是什麼原因導致一切都搞亂了。任何幫助,將不勝感激,我只需要一些線索,什麼是錯的。如果你想運行它,我發佈了該程序的鏈接。檢查功能不起作用
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));
printf("Math quiz. \n");
printf("This is level 1. There are a total of 10 questions. \n\n");
createQuestion();
checkQuestion();
}
//Functions
int integers(void) {
int digit;
digit = rand() % 10;
return digit;
}
char operations(void) {
int digit;
digit = rand() % 4;
if (digit == 0) {
return '+';
} else if (digit == 1) {
return '-';
} else if (digit == 2) {
return '*';
} else if (digit == 3) {
return '/';
}
}
int createQuestion(void) {
int i;
int count = 0;
int answer;
int sum;
for (i = 1; i <= 10; i++) {
count++;
printf("%d)%d", count, integers());
printf("%c", operations());
printf("%d", integers());
printf("=");
scanf("%f", & answer);
checkQuestion(integers(), integers(), operations(), answer);
}
}
void checkQuestion(float a, float b, float c, char d) { //a integer b integer c answer //d operator
int answer1;
int answer2;
int answer3;
int answer4;
if (operations() == '+') {
answer1 == a + b;
answer1 == c;
if (answer1 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '-') {
answer2 = a - b;
if (answer2 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '*') {
answer3 = a * b;
if (answer3 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '/') {
answer4 = a * b;
if (answer4 == c) {
return messagesGood();
} else {
return messagesBad();
}
}
}
void messagesGood(void) {
int digit;
digit = rand() % 4;
switch (digit) {
case 0:
printf("Very good! \n");
break;
case 1:
printf("Excellent! \n");
break;
case 2:
printf("Nice Work! \n");
break;
case 3:
printf("Keep up the good work! \n");
break;
}
}
void messagesBad(void) {
int digit;
digit = rand() % 4;
switch (digit) {
case 0:
printf("No. Please try again. \n");
break;
case 1:
printf("Wrong. Try once more. \n");
break;
case 2:
printf("Don’t give up! \n");
break;
case 3:
printf("No. Keep trying. \n");
break;
}
}
看,好試試。兩件事情。整數()函數每次都會返回一個隨機整數,對吧?所以當你在printf這個問題的時候調用它,當你調用它來傳遞一個參數到checkQuestion時,你認爲它們是兩個整數嗎?其次,在「checkQuestion」中評估'answer1'時,你會混淆使用'=='和'='。 – ncke