//猜数游戏 //程序运行时自动生成1——100之间的随机数,提示用户猜 //如果用户猜的数等于随机数,提示用户猜对了,程序结束 //否则,如果用户猜的数小于随机数,提示用户低了,用户继续猜 //如果用户猜的数大于随机数,提示用户高了,用户继续猜 //程序循环直到用户猜对为止 #include<stdio.h> #include<stdlib.h> #include<time.h> int main(){ int guessNumber;//存放程序运行时生成的1——100之间的随机整数 int ans; //存放用户猜的数 srand((time(0))); //以时间函数作为随机种子 guessNumber=1+rand()%100; //生成1——100之间的随机数 do{ printf("your guess number is(1——100):"); scanf("%d",&ans); if(ans<guessNumber) printf("%d is lower than the number.\n",ans); else if(ans>guessNumber) printf("%d higher than the number.\n",ans); }while(ans!=guessNumber); printf("congratulations. you hit it——\n"); system("pause");//在devc中运行时,这一行可以去掉 return 0; }
原文:https://www.cnblogs.com/201qx/p/11878372.html