猜数字游戏

void menu()
{
	printf("*****************\n");
	printf("***   1.play  ***\n");
	printf("***   0.exit  ***\n");
	printf("*****************\n");

}

void game()
{
	int guessnumber = 0;
	int r = rand() % 100 + 1;//1~100
	while (1)
	{
		printf("猜数字:>");
		scanf("%d", &guessnumber);
		if (guessnumber < r)
		{
			printf("猜小了\n");
		}
		else if (guessnumber > r)
		{
			printf("猜大了\n");
		}
		else
		{
			printf("猜中了笨比\n");
			break;
		}
	}


}

int main()
{
	int input = 0;
	srand((unsigned)time(NULL));
	do
	{
		menu();
		printf("请进行选择>:");
		scanf("%d", &input);
		
		switch(input)
		{
		case 1:
				game();
				break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}

	} while (input);
	


	return 0;
}

代码清晰简单。

你可能感兴趣的:(c语言,算法)