提问人:SortaSleepy 提问时间:10/29/2023 更新时间:10/29/2023 访问量:44
以前工作的变量在 C 中不再起作用
Previously working variables no longer working in C
问:
我正在对我的代码进行一些更改,因此它会随机给出加法、减法、乘法、除法(在循环遍历每个加法之前)。但出于某种原因,以前的工作变量(question、wrongAnswers、totalQuestions、correctAnswers)现在显示为未定义。是什么原因导致的,我该如何解决?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int addition(void){
int number1, number2, addAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
addAnswer = (number1 + number2);
printf("%d + %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == addAnswer){
return 1;
}
else{
return 0;
}
}
int subtraction(void){
int number1, number2, subAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
subAnswer = (number1 - number2);
printf("%d - %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == subAnswer){
return 1;
}
else{
return 0;
}
}
int multiplication(void){
int number1, number2, multAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
multAnswer = (number1 * number2);
printf("%d * %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == multAnswer){
return 1;
}
else{
return 0;
}
}
int division(void){
int number1, number2, divAnswer, response;
srand(time(NULL));
number1 = rand() % 12 + 1;
number2 = rand() % 12 + 1;
divAnswer = (number1 / number2);
printf("%d / %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == divAnswer){
return 1;
}
else{
return 0;
}
}
int main(void) {
int response, int question = 0, correctAnswers = 0, totalQuestions = 0, wrongAnswers = 0;
response = 0;
srand(time(NULL));
while (response != -1111) {
if (question == 0){
response = addition();
} else if (question == 1){
response = subtraction();
}else if (question == 2){
response = multiplication();
}else {
response = division();
}
if (response == 0){
wrongAnswers++;
totalQuestions++;
}
if (response == 1){
correctAnswers++;
totalQuestions++;
}
}
wrongAnswers = (totalQuestions - correctAnswers);
printf("Out of %d questions, you got %d right and %d wrong.\n", totalQuestions, correctAnswers, wrongAnswers);
return 0;
}
答:
2赞
NoDakker
10/29/2023
#1
在测试您的代码时,由于整数列表中无关的“int”声明,我立即收到编译器错误。
int response, int question = 0, correctAnswers = 0, totalQuestions = 0, wrongAnswers = 0;
第二个“int”需要省略。
int response, question = 0, correctAnswers = 0, totalQuestions = 0, wrongAnswers = 0;
然后,我能够编译程序。
接下来,在测试程序时,我注意到了两个问题。首先,对“-1111”的响应进行了测试,以便程序应该结束;但是,各种测试函数只会返回“0”或“1”的值,这意味着即使输入了“-1111”的答案,程序也不会正常结束。因此,为了解决这个问题,在每个函数中添加了一个小测试块,以返回“-1111”的值而不是“0”或“1”,以便进行正常的程序总结。
scanf("%d", &response);
if (response == -1111)
return response;
另一个问题是“question”变量被初始化为值“0”,因此只会进行加法测试。因此,为了允许进行其他操作测试,在每次迭代期间都会派生一个从“0”到“3”的随机值。
int main(void)
{
int response, question = 0, correctAnswers = 0, totalQuestions = 0, wrongAnswers = 0;
response = 0;
srand(time(NULL));
while (response != -1111)
{
question = rand() % 3; /* Otherwise, only addition questions appear */
通过这些重构,程序的测试运行就完成了。
craig@Vera:~/C_Programs/Console/RandomOperations/bin/Release$ ./RandomOperations
7 - 8 = ?
-1
6 + 1 = ?
7
10 + 9 = ?
19
6 * 9 = ?
54
1 * 10 = ?
22
6 * 0 = ?
0
11 - 6 = ?
5
2 + 0 = ?
-1111
Out of 7 questions, you got 6 right and 1 wrong.
这里最大的收获是查看编译器警告和/或错误,以查明代码中的问题。此外,在使用函数返回信息和循环条件时参考教程文献可能是明智的。
评论
0赞
NoDakker
10/29/2023
还有几件事需要注意 - 在除法函数中,可能会发生“除以零”,这可能应该加以防范。而且,仅查看代码的外观,就会发现每个函数中都复制了大量语句。可能,可以使用开关块将测试组合成一个函数,以辨别要问的问题类型。
评论
srand(time(NULL));
main
rand() % 12 + 1
1..12