<span style="font-family:Microsoft YaHei;font-size:24px;"></span><pre class="cpp" name="code">#include<stdlib.h>
#include<time.h>
#include<stdio.h>
int menu() //選擇菜單函數
{
int choose = 0;
printf("**************************\n");
printf("Please choose:\n");
printf("1.Game\n");
printf("0.EXIT\n");
printf("**************************\n");
while(1)
{
scanf("%d",&choose);
if(1 == choose)
{
return 1;//當選擇遊戲時返回1
}
else if(0 == choose)
{
return -1;//當選擇退出時返回0
}
else
{
printf("Please choose again!\n");//輸入其他值時重新輸入
}
}
}
void Game() //遊戲
{
int num = 0;
int r = 0;
srand( (unsigned)time( NULL ) );//調用srand函數,當時間不同,產生的隨機數不同
r = rand()%100; //調用rand函數用於產生隨機數
do
{
printf("Please input the number you guess:\n");
scanf("%d",&num);
if(num > r)
{
printf("It is big.\n");
}
else if(num < r)
{
printf("It is small.\n");
}
else
{
printf("Congratulations, you guessed it!\n");
break;
}
}while(1);//循環猜數
}
int main()
{
int ret = 0;
while(1)
{
ret = menu();//調用選擇菜單函數
if(1 == ret)
{
Game();
}
else
{
break;
}
}
return 0;
}
C語言實現猜數字遊戲
如有錯誤,歡迎指出!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.