網站首頁 健康小知識 母嬰教育 起名 運動知識 職場理財 情感生活 綠色生活 遊戲數碼 美容 特色美食 愛好
當前位置:酷知知識幫 > 遊戲數碼 > 電腦

c語言之猜數遊戲:[1]

欄目: 電腦 / 發佈於: / 人氣:1.38W

當我們大家一起出去遊玩或者聚會是會玩一種猜數字遊戲

方法/步驟1

(01)寫入所需頭文件#include <stdlib.h>/*隨機函數的申明*/#include <stdio.h> #include <time.h>

(02)所需變量的定義main(){int n,g,c=100,d=0,t;

(03)srand( (unsigned)time( NULL ) );n=rand()%100;    /*產生並調用隨機數*/

c語言之猜數遊戲:[1]

(04)程序其他部分printf("please input the number you guess between 0 to 100t");scanf("%d",&g);while(g!=n){if(g<n)  {   d=g;  printf("That was small,n please guess it again and input a number between %d to %dn",d,c);  scanf("%d",&t);g=t;}if(g>n) {c=g;  printf("That was large,n please guess it again    and input a number between %d to %dn",d,c) ;  scanf("%d",&t);g=t;} }if(g==n)printf("ttt|----------|nttt|you got it|nttt|----------|n");printf("nttt  Game overn");getch();}下如為運行結果

c語言之猜數遊戲:[1] 第2張

特別提示

注意頭文件的寫入