程序代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 5
int x, z, i=0;
int guess(void);
int main(void)
{
srand((unsigned int)time(NULL));
z = 1 + (int)(100.0 * rand() / (RAND_MAX + 1.0));
printf("随机生成的数字: %d\n", z);
printf("请输入你猜的数字:\n");
do{
scanf("%d", &x);
if (x < 0)
{
printf("Game Over\n");
exit(0);
}
guess();
} while (x!=z);
printf("猜了%d次\n", i);
return 0;
}
int guess(void)
{
if (i<=(MAX-1))
{
if (x==z)
{
switch (i+1)
{
case 1: printf("Bingo!\n");
break;
case 2: printf("Lucky You!\n");
break;
case 3: printf("Good Guess!\n");
break;
default: printf("You Win!\n");
}
}
else if (x>z)
{
printf("Too big!\n");
}
else
{
printf("Too small!\n");
}
}
else
{
printf("Game Over\n");
exit(0);
}
return i++;
}
[
本帖最后由 longwu9t 于 2014-11-23 15:55 编辑 ]