1. 程式人生 > >完成猜數字遊戲。

完成猜數字遊戲。

完成猜數字遊戲。 

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int menu()
{
	int choose = 0;
	printf("************************************\n");
	printf("********  Please choose:   *********\n");
	printf("********  1.Game   0.Exit  *********\n");
	printf("************************************\n");
	while(1)
	{
		scanf("%d",&choose);
		if(1 == choose)
		{
			return 1;
		}
		else if(0 == choose)
		{
			return -1;
		}
		else
		{
			printf("Please choose again!\n");
		}
	}
}

void game()
{
	int num = 0;
	int ret = 0;
	srand((unsigned) time (NULL));
	ret = rand()%100;
	do
	{
		printf("Please input the number you guess:\n");
		scanf("%d",&num);
		if(num > ret)
		{
			printf("It is big.\n");
		}
		else if(num < ret)
		{
			printf("It is small.\n");
		}
		else
		{
			printf("Congratulations, you guessed it!\n");
			break;
		}

	}while(1);
}
int main()
{
	int r = 0;
	while(1)
	{
		r = menu();
		if(1 == r)
		{
			game();
		}
		else
		{
			break;
		}
	}
	return 0;
}