溫馨提示×

c語言猜數字1到100游戲怎么實現

小億
360
2023-08-15 18:39:01
欄目: 編程語言

以下是一個實現C語言猜數字1到100游戲的示例代碼:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
// 生成隨機數
srand(time(NULL));
number = rand() % 100 + 1;
printf("歡迎參加猜數字游戲!\n");
printf("猜一個介于1到100之間的數字。\n");
do {
printf("請輸入你的猜測:");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("猜大了!\n");
} else if (guess < number) {
printf("猜小了!\n");
} else {
printf("恭喜你,猜中了!\n");
printf("你猜了 %d 次。\n", attempts);
}
} while (guess != number);
return 0;
}

這個程序的基本思路是:生成一個1到100之間的隨機數,然后循環接受用戶的輸入猜測,根據用戶的猜測輸出相應的提示,直到用戶猜中為止。程序記錄了用戶猜測的次數,并在猜中時輸出猜測次數。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女