日勿雨林

關於部落格
請前往新站:ian.twgg.org
  • 14079

    累積人氣

  • 0

    今日人氣

    0

    訂閱人氣

猜數字遊戲



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int a,b,g[2],i=1;
    g[0]=1;
    g[1]=100;
    /**產生亂數**/
    srand(time(NULL));
    a = (rand()%100)+1;
    printf("     請猜一 1~100 的數:");
    scanf("%d",&b);
    /**迴圈開始**/
    while(1){
             /**判斷輸入輸字是否溢位**/
             if(((g[1]-b)>0) && (b-(g[0])>0)){
                                             /**判斷方向**/
                                          if((b-a)>0){
                                                        printf("範圍: %d~%d`;",g[0],b);
                                                        g[1] = b;
                                                      }
                                                      else
                                                      {
                                                        printf("範圍: %d~%d;",b,g[1]);
                                                        g[0]=b;
                                                      }
                                             }
                                             else
                                             {
                                             printf("輸入數字不在範圍內");
                                             i--;
                                             }       
             printf("請再輸入:");
             scanf("%d",&b);
             i++;
             /**判斷是否已正確**/  
             if(a==b){
                      printf("猜對了!n");
                      printf("共猜了%d次!n",i);
                      break;
                        
                      }               
            }
    system("PAUSE");   
}
相簿設定
標籤設定
相簿狀態