void result_1(){
int score = 0;
char answer[8];
do
{
scanf("%s", answer);
} while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
if (strcmp(answer, "c") == 0)
{
score += 1;
printf("Correct \tYour score is now %d\n", score);
}
else
{
printf("Wrong \tYour score is still %d\n", score);
}
}
void result_2(){
int score = 0;
char answer[8];
do
{
scanf("%s", answer);
} while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
if (strcmp(answer, "a") == 0)
{
score += 1;
printf("Correct \tYour score is now %d\n", score);
}
else
{
printf("Wrong \tYour score is still %d\n", score);
}
}
void result_3(){
int score = 0;
char answer[8];
do
{
scanf("%s", answer);
} while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
if (strcmp(answer, "c") == 0)
{
score += 1;
printf("Correct \tYour score is now %d\n", score);
}
else
{
printf("Wrong \tYour score is still %d\n", score);
}
}
void result_4(){
int score = 0;
char answer[8];
do
{
scanf("%s", answer);
} while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
if (strcmp(answer, "d") == 0)
{
score += 1;
printf("Correct \tYour score is now %d\n", score);
}
else
{
printf("Wrong \tYour score is still %d\n", score);
}
}
void result_5(){
int score = 0;
char answer[8];
do
{
scanf("%s", answer);
} while (strcmp(answer, "a") != 0 && strcmp(answer, "b") != 0 && strcmp(answer, "c") != 0 && strcmp(answer, "d") != 0);
if (strcmp(answer, "c") == 0)
{
score += 1;
printf("Correct \tYour score is now %d\n", score);
}
else
{
printf("Wrong \tYour score is still %d\n", score);
}
}
int main(void)
{
int score = 0;
char answer[8];
printf("Welcome to the C brain scan\n");
printf("I will ask you some questions...\n");
printf("Select the best answer and type in the corresponding letter.\n");
printf("1. What does the == operator do?\n");
printf("(a) calculates an arithmetic solution.\n");
printf("(b) assigns a value to a variable.\n");
printf("(c) checks for equality\n(d) draws the '=' character\n");
printf("Your answer: \n");
result_1();
printf("2. Which is NOT a C keyword?\n");
printf("(a) when\n");
printf("(b) const\n");
printf("(c) unsigned\n(d) do\n");
printf("Your answer: \n");
result_2();
printf("3. In function call, the actual parameters are separated by\n");
printf("(a) semicolons\n");
printf("(b) colons\n");
printf("(c) commas\n(d) space\n");
printf("Your answer: \n");
result_3();
printf("4. What is the keyword for a global variable?\n");
printf("(a) global\n");
printf("(b) namespace\n");
printf("(c) external\n(d) there is no keyword. to be global the variable must be declared outside of main()\n");
printf("Your answer: \n");
result_4();
printf("5. A function prototype always contains:\n");
printf("(a) a void type\n");
printf("(b) an integer parameter\n");
printf("(c) a function name\n(d) a comma\n");
printf("Your answer: \n");
result_5();
}
以上是我的完整代码
结果是这样的:
如果答案正确,得1分,否则得分不变。
我的问题:
我想把重复代码放在函数内部,使主函数看起来更干净。
在测验1中,如果我回答正确,我得到1分,但当我回答测验2时,即使我回答正确,也没有加分。
2条答案
按热度按时间ffscu2ro1#
您的代码不完整,main的底部缺失。
在提供的代码中,你只运行了一次测验,如果你回答了c,那么你得到一分。如果(我在这里假设),完整的代码有另一个对result_1()的调用,并且你再次正确回答,输出仍然只有1。
为什么?因为你的score变量是局部的,在每次函数调用后就失去了作用域。而且,它在每次函数调用中都被初始化为0,但主要的收获是它是局部的。
请提供完整的代码,以便我们能够提供帮助。
thigvfpy2#
如何在C中使用函数?函数声明/原型的一般格式是:例如
<Return type> <function name> <arguments>;
,void quiz1(void);
将不返回任何值,也不接受任何参数,而void get_n_check_answer(enum quizid id);
将接受1个参数,与上一个示例相反。在原型之后,您只需在函数体中定义函数应该做什么。C中的函数体看起来像<Return type> <function name> <arguments> {... BODY ....}
因此,quiz 1()的函数体看起来像这样void quiz1(void) {...Body of code....}
现在让我们进入代码:
提供的代码的输出:
希望这对你有帮助。如果你有任何问题,不要犹豫,问。