我的简单C程序是一个随机数游戏.问题是随机数总是一样的,即使有srand(时间(NULL))


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

main() {
int n,counter=0,choice;
srand(time(NULL));
n = rand() % 10 + 1;
printf("Geuss My Numbern");
while(1){
counter++;
scanf("%d" ,&n);
if(n==choice){
printf("Correct You Guessed It in %d Triesn" ,counter);
break;
}
else if (n>choice) printf("Too Highn");
else printf("Too Lown");
}

}

例如,每次我运行程序时,随机数总是不变的。

我想你是这个意思;

scanf("%d" ,&n); -->  scanf("%d" ,&choice);

您的scanf正在覆盖您的随机数n

更改:

scanf("%d" ,&n);

进入:

scanf("%d" ,&choice);

最新更新