c语言循环结构写猜数游戏
时间: 2023-10-26 11:28:42 浏览: 161
C语言编写的猜数游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int secretNumber, guess, numGuesses = 0;
srand(time(NULL));
secretNumber = rand() % 100 + 1; // 生成1~100之间的随机数
printf("***欢迎来到猜数游戏!***\n");
printf("猜测一个1~100之间的整数。\n");
do {
printf("请输入你的猜测:");
scanf("%d", &guess);
numGuesses++;
if (guess > secretNumber) {
printf("你猜测的数太大了,请继续猜测。\n");
} else if (guess < secretNumber) {
printf("你猜测的数太小了,请继续猜测。\n");
} else {
printf("恭喜你,猜对了!\n");
printf("你总共猜了%d次。\n", numGuesses);
}
} while (guess != secretNumber);
return 0;
}
阅读全文