随机数(加法游戏)问题
Random Number (addition game) issue
我正在为我的 C class 入门制作一个 addition/multiplication 游戏。该程序的 objective 是询问用户,您想要使用的最大数量是多少,它将在该最大范围内播种随机数,以及您想要做多少不同的问题。当我 运行 程序并进行数学计算时,它告诉我总和不正确,并提供了一个不正确的答案,通常是一个很大的数字,如“1254323”。你能指出我做错了什么的正确方向吗?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
//printf("Would you like 1)Addition or 2)Multiplication?\n")
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
sum = num1 + num2;
while(i != problems) {
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
i++;
printf("What is %d + %d\n",num1, num2);
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
}
else{
printf("Correct!\n");
}
}
return 0;
}
您在设置 num1
和 num2
之前分配 sum
。
将 sum = num1 + num2;
行移动到 num2 = rand()%maxNumber;
行之后的循环中,它应该可以正常工作。
还有一些其他错误(例如用 0 初始化 i
)。
顺便说一句,通常认为使用 for
循环而不是 while
循环是更好的做法。
这里是更易读的代码
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
for (i = 0; i < problems; i++) {
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
sum = num1 + num2;
printf("What is %d + %d\n",num1, num2);
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
} else {
printf("Correct!\n");
}
}
return 0;
}
您在使用变量时未对其进行初始化。变化:
int maxNumber, num1, num2, sum, answer, problems, i;
收件人:
int maxNumber = 0, num1 = 0, num2 = 0, sum = 0, answer = 0, problems = 0, i = 0;
此外,将 sum = num1 + num2;
行移动到创建 num1
和 num2
的位置之后。
您的程序存在多个问题,包括格式问题。以下是更正:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
//printf("Would you like 1)Addition or 2)Multiplication?\n")
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
// issue: i was not initialized
i = 0;
while(i != problems){
i++;
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
printf("What is %d + %d\n", num1, num2);
// issue: sum was not calculated here
sum = num1 + num2;
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
}
else{
printf("Correct!\n");
}
}
return 0;
}
您定义 sum = num1 + num2,其中 num1 和 num2 根本没有赋值。默认情况下,C 不会将数字初始化为零。这就是为什么你有这么大的数字。
只需在 num2 = rand()%maxNumber;
之后添加 sum = num1 + num2
,一切正常!
我正在为我的 C class 入门制作一个 addition/multiplication 游戏。该程序的 objective 是询问用户,您想要使用的最大数量是多少,它将在该最大范围内播种随机数,以及您想要做多少不同的问题。当我 运行 程序并进行数学计算时,它告诉我总和不正确,并提供了一个不正确的答案,通常是一个很大的数字,如“1254323”。你能指出我做错了什么的正确方向吗?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
//printf("Would you like 1)Addition or 2)Multiplication?\n")
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
sum = num1 + num2;
while(i != problems) {
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
i++;
printf("What is %d + %d\n",num1, num2);
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
}
else{
printf("Correct!\n");
}
}
return 0;
}
您在设置 num1
和 num2
之前分配 sum
。
将 sum = num1 + num2;
行移动到 num2 = rand()%maxNumber;
行之后的循环中,它应该可以正常工作。
还有一些其他错误(例如用 0 初始化 i
)。
顺便说一句,通常认为使用 for
循环而不是 while
循环是更好的做法。
这里是更易读的代码
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
for (i = 0; i < problems; i++) {
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
sum = num1 + num2;
printf("What is %d + %d\n",num1, num2);
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
} else {
printf("Correct!\n");
}
}
return 0;
}
您在使用变量时未对其进行初始化。变化:
int maxNumber, num1, num2, sum, answer, problems, i;
收件人:
int maxNumber = 0, num1 = 0, num2 = 0, sum = 0, answer = 0, problems = 0, i = 0;
此外,将 sum = num1 + num2;
行移动到创建 num1
和 num2
的位置之后。
您的程序存在多个问题,包括格式问题。以下是更正:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int maxNumber, num1, num2, sum, answer, problems, i;
srand(time(NULL));
//printf("Would you like 1)Addition or 2)Multiplication?\n")
printf("Enter your Max Number:");
scanf("%d", &maxNumber);
printf("How many problems do you want?\n");
scanf("%d", &problems);
// issue: i was not initialized
i = 0;
while(i != problems){
i++;
num1 = rand()%maxNumber;
num2 = rand()%maxNumber;
printf("What is %d + %d\n", num1, num2);
// issue: sum was not calculated here
sum = num1 + num2;
scanf("%d", &answer);
if(answer != sum){
printf("Sorry, that's incorrect, the answer is %d\n", sum);
}
else{
printf("Correct!\n");
}
}
return 0;
}
您定义 sum = num1 + num2,其中 num1 和 num2 根本没有赋值。默认情况下,C 不会将数字初始化为零。这就是为什么你有这么大的数字。
只需在 num2 = rand()%maxNumber;
之后添加 sum = num1 + num2
,一切正常!