本文目錄一覽:
C語言編寫一個猜字遊戲,,,遇到了問題· 請教高人
把這句話放循環外。
printf(“很遺憾,你沒猜中本次中獎號碼,謝謝你的參與:!\n”);
另外,新定義個flag,表示被猜中了。初始化為false。
如果猜中,就在恭喜那個程序塊中設置為true。
然後在很遺憾那句前面加上對這個flag的判斷,如果=false,則輸出很遺憾這句話
C語言猜字程序,幫忙修改下
//—————————————————————————
#include stdio.h
#include ctype.h
int main(void)
{
int choose=15;
int guess=0;
int count=3;
char another_game=’Y’;
printf(“\nThis is a guess game.”);
printf(“\nI have choose a number between 1 to 20 which you must guess\n”);
printf(“Press enter to play the game.”);
scanf(“%c”,another_game);
do
{
for(count=3;count0;–count) /**********************注意這裡************************/
{
printf(“\nYou have %d tr%s left.”,count,count==1?”y”:”ies”);
printf(“\nPlease enter a guess:”);
scanf(“%d”,guess);
if(guess==choose)
{
printf(“\nYou guess it!”);
return 0;
}
else
{
if(guess0||guess20)
{
printf(“\nI said guess a number between 1 to 20!”);
}
else
{
printf(“\nSorry,you give a wrong number!”);
}
}
}
printf(“\nWould you like to try it again? Y or N:”);
fflush(stdin); /**********************注意這裡************************/
scanf(“%c”,another_game);
}while(toupper(another_game)==’Y’); /**********************注意這裡************************/
return 0;
}
//—————————————————————————
設計一個猜字遊戲的C語言程序
以下程序的功能是隨機產生數字,要求用戶猜測程序中產生的隨機數字,並輸入,根據猜測的結果程序給出不同的響應,如果15次沒猜對則退出。
源程序如下:
#include stdio.h
#includestdlib.h
#includectype.h
main()
{
int count;/*猜數字的次數*/
int number;/*系統產生的隨機數字*/
int guess;/*程序員輸入數字*/
char yes=’Y’;
clrscr();
printf(“\nNow let us play the game.\n Guess the number:”);
while (toupper(yes)==’Y’)
{
count=0;
randomize();
number=random(100)+1;
do
{
do
{
printf(“\nInput an integer number(1~100):”);
scanf(“%d”,guess);
}while(!(guess=1guess=100));/*結束第二層DO~WHILE循環*/
if (guessnumber)
printf(“\n Your answer is low,try again!”);/*如果用戶輸入的數字小於系統隨機數,則輸出數字太小的提示信息*/
if (guessnumber)
printf(“\n Your answer is high,try again!”);/*如果用戶輸入的數字大於系統隨機數,則輸出數字太小的提示信息*/
count++;/*猜測次數加一*/
if (count==15)
{
printf(“\n This is the %d times! Think it hard next!”,count);
exit(0);/*如猜測15次還沒猜對,則退出遊戲*/
}
}while (!(guess==number));
if (count=7)/*猜測的次數小於7次*/
{
printf(“\n You have got it in %d times.\n”,count);
printf(“\n you guess right,Congretulations!”);/*遊戲成功則提示祝賀信息*/
}
else
{
printf(“\n You got it in %d times.\n”,count);
printf(“\n I bet you can do it better!”);/*遊戲失敗則提示鼓勵信息*/
}
printf(“\n NEXT?(Y/N):”);/*選擇是否重新遊戲*/
scanf(“%c”,yes);
}
}
運行程序時請用戶猜數字,該數字由系統隨機產生,用戶最多有七次猜測的機會,如果在七次內猜對數字,則程序顯示祝賀信息,如果用戶大於七次猜對數字,則程序顯示鼓勵信息,如果用戶連續15次都沒有猜對數字,則遊戲自動退出。結束一次遊戲後,系統詢問用戶進行下一次猜數字遊戲,用戶輸入“Y”則開始下一次猜數字遊戲,用戶如果輸入“N”則退出遊戲。
c語言猜字問題
這裡的k只有1或0兩種可能值,所以char和int或者bool都行;
這樣寫的人可能覺得用char更節省內存,然而實際沒用;
初學者重要的是自己寫一遍,而不是糾結於細節。細節以後你看書多了寫的多了自然會明白的。
原創文章,作者:NSGE,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/147665.html