我想弄清楚我的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