當前位置:編程學習大全網 - 編程軟體 - c語言,使用隨機函數rand()編寫壹個猜數字的遊戲程序

c語言,使用隨機函數rand()編寫壹個猜數字的遊戲程序

測試數據:

請輸入您猜的那個數:

10

第1次猜測,很遺憾您猜小了!

請輸入您猜的那個數:

15

第2次猜測,很遺憾您猜大了!

請輸入您猜的那個數:

12

第3次猜測,恭喜您猜對了,電腦隨機數是12!

代碼:

#include "stdio.h"

#include "conio.h"

#include <time.h>

#include <math.h>

#include<stdlib.h>

main()

{

int randnum,count=0;

int n,isRight;

time_t t;

srand((unsigned)time(&t));

randnum=(int)(rand()%1000);

while(1)

{

printf("\n請輸入您猜的那個數:\n");

scanf("%d",&n);

count++;

if(n==randnum)

{

printf("第%d次猜測,恭喜您猜對了,電腦隨機數是%d!\n",count,randnum);

break;

}

else if(n>=randnum)

{

printf("第%d次猜測,很遺憾您猜大了!\n",count);

}

else

{

printf("第%d次猜測,很遺憾您猜小了!\n",count);

}

}

getch();

}

getch();

}

樓主好運!

  • 上一篇:在計算機編程中 true和false如何使用才是正確的呢 請高手講解壹下
  • 下一篇:圖形與幾何的知識點有哪些?
  • copyright 2024編程學習大全網