首先輸入1表示開始游戲,輸入0表示退出游戲,輸入其他數(shù)字提示錯(cuò)誤;
接下來(lái),輸入一個(gè)猜的數(shù)字,計(jì)算機(jī)提示你猜大了或是猜小了,直至猜正確。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void menu()
{
printf("**********************\n");
printf("****1.play? 0.exit****\n");
printf("**********************\n");
}
void play_game()
{
int tmp = 0;
int rand_n = rand() % 100;? ? ? ? ? //使隨機(jī)數(shù)的大小在0-100之間
while (1)
{
? printf("請(qǐng)輸入你猜的數(shù)>>:");
? scanf("%d", &tmp);
? if (tmp == rand_n)
? {
? printf("恭喜,猜對(duì)了\n");
? break;
? }
? else if (tmp > rand_n)
? {
? printf("猜大了\n");
? }
? else
? {
? printf("猜小了\n");
? }
}
}
int main()
{
int input;
srand((unsigned int)time(NULL));? ? ? ? //產(chǎn)生一個(gè)隨機(jī)數(shù)
do
{
? menu();
? printf("請(qǐng)選擇>>:");
? scanf("%d", &input);
? switch (input)
? {
? case 1:
? {
? ? ? play_game();
? ? ? break;
? }
? case 0:
? {
? ? ? exit(EXIT_SUCCESS);
? ? ? break;
? }
? default:
? {
? ? ? ? ? ? ? ? printf("error\n");
? ? ? continue;
? }
? }
} while (input);
return 0;