欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c语言小程序:编写猜字游戏

程序员文章站 2022-06-24 20:14:34
#include #include #include int main()...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	int input=1;
	printf("欢迎使用猜数字游戏\n");
	while (input)
	{
	    printf("**********************\n");
	    printf("******* 1.start ******\n");
	    printf("******* 0. exit ******\n");
	    printf("**********************\n");
		printf("请选择>");
		scanf("%d", &input);
                //开始玩游戏
		switch (input)
		{
		case 1:
		{
			int n=0;
			int ch=0;
		srand((unsigned int)time(NULL));//随机值产生之前先设定一个种子(time)
			n=rand()%101;//随机产生一个1-100之间的数
			while(1)
			{
				printf("请猜一个(0-100)直接的数\n");
				scanf("%d", &ch);
				if(ch>n)
				{
					printf("你猜大了\n");
				}
				else if(ch<n)
				{
					printf("你猜小了\n");
				}
				else
				{
					printf("恭喜你,猜对了\n");
					break;
				}
			}
			break;
		}
		default:
			printf("游戏退出\n");
			break;
		}
	}
	return 0;
}