

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()//主函式
{
int magic,guess,counter;//magic代表電腦亂數,guess代表用戶猜的數,counter代表用戶猜的次數
char reply; //定義字符型變數reply,代表用戶是否繼續游戲
while(1)
{
srand(time(NULL)); //生成亂數種子
while(1)
{
magic=rand()%100+1; //生成亂數
counter=0; //次數開始為0
while(1)
{
printf("請輸入number=");//用戶輸入資料
scanf("%d",&guess);
counter++;
if(guess>magic)
{
printf("Wrong!Too high");//輸出提示用戶輸入資料太高
}
else if(guess<magic)
{
printf("Wrong!Too low");//輸出提示用戶輸入資料太低
}
else
{
printf("Right!");//輸出正確
}
if((guess!=magic)&&(counter>10))//當用戶猜想的資料錯誤,且猜想次數不大于10次,則繼續猜
{
break;
}
}
printf(" 一個數最多猜10次,您猜的次數counter=%d\n",counter);//輸出用戶猜的次數
printf("Do you want to continue( y/n)?輸入y代表繼續,n代表結束游戲");//是否繼續猜下一個數
scanf("%1s",&reply);
if(reply!='y')
break;
}
printf("the game is over");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78786.html
標籤:C語言
上一篇:合并果子問題一直wrong
