天天看點

C語言猜數字小遊戲

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
  int x,num,st,ed;
  char ch;
  srand((int)time(NULL));
  while(1)
  {
    printf("請輸入你想要猜的數的範圍(用空格隔開):");
    scanf("%d %d",&st,&ed);
    ed=ed-st+1;
    num=rand()%ed+st;
    printf("請輸入你猜的數字:");
    scanf("%d",&x);
    while(x!=num)
    {
      if(x>num)
      printf("猜大了,繼續努力~~\n");
      if(x<num)
      printf("猜小了,繼續加油哦!\n");
      scanf("%d",&x);
    } 
    printf("真棒!猜對了!!\n");
    printf("繼續玩請輸入Y,否則請輸入N\n");
    getchar();
    scanf("%c",&ch);
    if(ch=='Y')
    continue;
    else
    break;
  }
  return 0;
  
}