猜数字游戏是令游戏机随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,则提示“Game Over”,并结束程序。如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序。用c语音
时间: 2023-06-10 07:08:05 浏览: 51
以下是用C语言编写的猜数字游戏程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUM 100
#define MAX_TRIES 7
int main()
{
int secret_num, guess, tries = 0;
srand(time(NULL));
secret_num = rand() % MAX_NUM + 1;
printf("Welcome to the Guessing Game!\n");
printf("I'm thinking of a number between 1 and %d.\n", MAX_NUM);
while (tries < MAX_TRIES) {
printf("Enter your guess (try %d/%d): ", tries + 1, MAX_TRIES);
scanf("%d", &guess);
if (guess < 0) {
printf("Game Over\n");
break;
} else if (guess == secret_num) {
if (tries == 0) {
printf("Bingo!\n");
} else if (tries <= 2) {
printf("Lucky You!\n");
} else {
printf("Good Guess!\n");
}
break;
} else if (guess < secret_num) {
printf("Too small.\n");
} else {
printf("Too big.\n");
}
tries++;
}
if (tries == MAX_TRIES) {
printf("Game Over\n");
}
return 0;
}
```
程序使用`rand()`函数生成一个1到100之间的随机数,然后在主循环中,用户输入猜测的数,程序计算猜测的次数,并与随机数进行比较,输出提示。如果猜测的数等于随机数,则输出相应的提示信息并结束程序。如果猜测的次数达到了最大次数,并且仍未猜中随机数,则输出“Game Over”并结束程序。如果用户输入了一个负数,则也输出“Game Over”并结束程序。