当计算机选择1到100之间的数字时,我需要编写一个播放游戏“猜数字”的程序,并且用户有10次尝试,如果他猜测的数字太小,则用户每次都会收到输出或者太大,或者如果它是幸运数字,请帮助
这就是我到目前为止所做的......
#include <stdio.h>
#include <stdlib.h>
void main() {
int i ,guess=0 , lucky=rand()%100;
for (i = 0; i <= 10; i++) {
printf("please enter your guess\n");
scanf("%d",&guess);
}
}
答案 0 :(得分:2)
这对你有用!
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int count , guess = 0, lucky = rand() % 101;
for (count = 0; count < 10; count++) {
printf("please enter your guess\n>");
scanf("%d", &guess);
if(guess == lucky) {
printf("WIN");
break;
}
if(guess > lucky)
printf("too big\n\n");
else
printf("too small\n\n");
}
if(count == 10)
printf("FAIL");
return 0;
}