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;
}