关于C#:随机数(加法游戏)问题

Random Number (addition game) issue

我正在做一个加法/乘法游戏,为我的C课介绍。这个程序的目标是询问用户,您想要使用的最大值是什么,它将在最大范围内随机设定数字,以及您想要做多少不同的问题。当我运行程序进行数学运算时,它告诉我求和不正确,并提供了一个不正确的答案,通常是一个大数字,如"1254323"。你能指出我做错了什么吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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?
")
    printf("
Enter your Max Number:");
    scanf("
%d", &maxNumber);

    printf("
How many problems do you want?
");
    scanf("
%d", &problems);

    sum = num1 + num2;

    while(i != problems) {
        num1 = rand()%maxNumber;
        num2 = rand()%maxNumber;
        i++;

        printf("
What is %d + %d
",num1, num2);
        scanf("
%d", &answer);

        if(answer != sum){
            printf("
Sorry, that's incorrect, the answer is %d
", sum);
        }

        else{
            printf("Correct!
");
        }
    }

    return 0;
}


您使用的变量没有初始化它们。更改:

1
int maxNumber, num1, num2, sum, answer, problems, i;

到:

1
int maxNumber = 0, num1 = 0, num2 = 0, sum = 0, answer = 0, problems = 0, i = 0;

另外,将sum = num1 + num2;行移到创建num1num2之后。


在设置num1num2之前,您要分配sum

sum = num1 + num2;线移到num2 = rand()%maxNumber;线后的回路中,应能正常工作。

还有一些其他错误(例如用0初始化i)。

顺便说一句,通常认为使用for循环而不是while循环是更好的做法。

这里的代码可读性稍高一些

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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?
"
);
  scanf("%d", &problems);

  for (i = 0; i < problems; i++) {
    num1 = rand()%maxNumber;
    num2 = rand()%maxNumber;
    sum = num1 + num2;

    printf("What is %d + %d
"
,num1, num2);
    scanf("%d", &answer);

    if(answer != sum){
      printf("Sorry, that's incorrect, the answer is %d
"
, sum);
    } else {
      printf("Correct!
"
);
    }
  }

  return 0;
}


您定义sum=num1+num2,其中num1和num2根本没有被分配。默认情况下,C不会初始化数字为零。这就是为什么你有这么大的数字。

num2 = rand()%maxNumber;之后加上sum = num1 + num2,一切都会好起来的!


您的程序存在多个问题,包括格式设置。以下是更正:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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?
")
    printf("
Enter your Max Number:");
    scanf("
%d", &maxNumber);

    printf("
How many problems do you want?
");
    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
", 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
", sum);
        }
        else{
            printf("Correct!
");
        }
    }
    return 0;
}