請輸入您猜的那個數:
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();
}
樓主好運!