1. 程式人生 > >【C】用c語言編寫一個猜字遊戲!!!!

【C】用c語言編寫一個猜字遊戲!!!!

首先,編寫一個猜字遊戲需要使用者選擇頁面

其次是,在遊戲過程中如果猜錯就需要重新輸入(即需要用到迴圈結構)

當用戶猜對了,就需要停止程式。(使用break)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
void menu()
{
	printf("******************************\n");
	printf("****1:play       0:exit   ****\n");
    printf("******************************\n");
}
void play_game()
{
	int  fog = 0;
	int  random = 0;
	
	random=rand()%101;
	while(1)
	{
		printf("請輸入一個數:");
		scanf("%d",&fog);

	if(random==fog)
	{
		printf("恭喜你,答對了\n");
		break;
	}
	else if (fog>random)
	{
		printf("你輸入的大了\n");
	}
	else
	{
        printf("你輸入的小了\n");
	}
    }

}
int main()
{
	int input;
	srand((unsigned )time(NULL));
	do
	{
		menu();
		printf("請輸入你的選擇:");
		scanf("%d",&input);
		switch(1)
		{
		case 1:
          play_game();
		  break;
		case 0:
			exit(EXIT_SUCCESS);
		default:
			printf("error");
			break;
		}
	}while(input);
	return 0;
         
}