1. 程式人生 > 實用技巧 >C語言------猜數字遊戲

C語言------猜數字遊戲

C語言編寫猜數字遊戲

猜數字遊戲簡單描述:
1.根據提示資訊輸入choice的值,若是1,則開始遊戲;若是2,則退出遊戲.
2.當遊戲開始時,輸入[0,100]內的任意一個數字.如果輸入的數字input與要猜的數字toGuess相同時,則輸出猜中了(A correct guess);如果輸入的數字input小於要猜的數字toGuess時,則輸出猜低了(low);若果輸入的數字input大於要猜的數字toGues時,則輸出猜高了(high);
原始碼:
void game(){
printf(“遊戲開始了\n”);
//toGuess表示要去猜的數字
int toGuess=rand()%100+1;
// input表示輸入的數字

int input = 0;
while (1){
printf(“please enter number:”);
scanf("%d", &input);
if (input == toGuess){
printf(“A correst guess\n”);
break;
}
else if (input < toGuess){
printf(“low!\n”);
}
else {
printf(“high!\n”);
}
}
}

int main()
{
srand(time(0));
while (1){
printf("-----------------------------\n");

printf(“1.start game \n”);
printf(“2.end game \n”);
printf("-----------------------------\n");
printf(“please choose 1/2:”);
int choice=0;
scanf("%d", &choice);
if (choice == 1){
game();
}
else if (choice == 2){
printf(“exit game!\n”);
break;
}
else{
printf(“enter error\n”);
}
}
system(“pause”);
return 0;
}

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述