我试图找出我的checkQuestion
函数出了什么问题。我不知道为什么,但是有时它不会检查某些问题或完全跳过它们。我创建了一个函数createQuestion
,并在checkQuestion
中调用了createQuestion
,所以我不知道这是否是导致一切混乱的原因。任何帮助将不胜感激,我只需要一些提示就可以解决问题。如果您想运行该程序,我会将其链接发布到该程序。
https://repl.it/MQsD/142
#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;
}
}
最佳答案
每次调用operations
时,都会得到不同的结果。您需要根据自己的条件与d
进行比较,而不是与operator
进行比较。您在createQuestion
中有一个类似的问题,其中传递给checkQuestion
的内容可能不是显示给用户的内容。
例如:
if (d == '+') {
// ...
}