#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void play_game()//猜數字游戲實作
{
//1.生成一個亂數
int ret=rand()%100+1;//設定了隨機種子之后配合rand產生一個亂數,生成范圍:0-32769
//但rand生成的范圍過大,對100取模后+1,使隨機范圍為1-100
while (1)
{
printf("請輸入你猜的數:>\n");
int a = 0;
scanf("%d", &a);
if (a > ret)
{
printf("你猜的數字大了,請重新猜數字:>\n");
}
else if (a < ret)
{
printf("你猜的數字小了,請重新猜數字:>\n");
}
else
{
printf("恭喜你,猜對了\n");
printf("謎底是 %d \n", ret);
break;
}
}
}
void menu()//選單
{
printf("********************************\n");
printf("**********1.play game***********\n");
printf("**********0.exit****************\n");
printf("********************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//time(NULL)作用是回傳一個現在時間秒數,回傳型別是time_t
//而srand的型別為unsigned int,所以需要強制型別轉換
//srand頭檔案為 <stdlib.h>,time頭檔案為 <time.h>
do
{
menu();
printf("請輸入一個數:>");
scanf("%d", &input);
switch (input)
{
case(1):
printf("即將進入猜數字游戲:>\n");
play_game();
break;
case(0):
printf("成功退出游戲!\n");
break;
default:
printf("輸入錯誤,請重新輸入:>\n");
break;
}
} while (input);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/414007.html
標籤:其他
下一篇:GMM-HMM聲學模型實體詳解
