C语言------猜数字游戏

it2025-12-18  11

猜数字游戏

简单描述 1.根据提示信息输入choice的值,若是1,则开始游戏;若是2,则退出游戏. 2.当游戏开始时,输入[0,100]内的任意一个数字.如果输入的数字input与要猜的数字toGuess相同时,则输出猜中了(A correct guess);如果输入的数字input小于要猜的数字toGuess时,则输出猜低了(low);若果输入的数字input大于要猜的数字toGues时,则输出猜高了(high); 源代码::在这里插入代码片

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<Windows.h> #include<stdlib.h> #include<time.h> 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))表示设置随机种子,使用当前时间设置的.因为时间是不断变化的,随机种子也就不断变化 //若设定的随机种子是固定的,如srand(0),srand(1)等等,那么产生的随机数序列就是固定的,你这次猜中了15,结束游戏后,下次重新运行进入游戏后,输入15就有猜中了 //但是用时间设置随机种子,当这次玩完游戏关掉后重新运行,它会根据时间的不同产生不同的随机种子 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; }

最新回复(0)