用c语言写一个猜数字的游戏
时间: 2023-11-24 19:05:40 浏览: 56
用C语言制作一个猜数字游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int answer, guess, count = 0;
srand(time(NULL));
answer = rand() % 100 + 1;
printf("猜数字游戏,数字在1到100之间\n");
do {
printf("请猜一个数字:");
scanf("%d", &guess);
count++;
if (guess > answer) {
printf("猜大了,请再猜一次\n");
} else if (guess < answer) {
printf("猜小了,请再猜一次\n");
}
} while (guess != answer);
printf("恭喜你,猜对了,答案是%d,你一共猜了%d次\n", answer, count);
return 0;
}
阅读全文