猜数字
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#pragma warning(disable:4996)
void game()
{
int a = 0;
int b = 0;
scanf("%d %d ", &a, &b);
int c = a + rand() % (b - a + 1);
int guess = 0;
int d = 5;
while (1)
{
printf("您还有%d机会\n", d);
printf("请说数字:\n");
scanf("%d", &guess);
if (guess < c)
{
printf("猜小了\n");
}
else if (guess > c)
{
printf("猜大了\n");
}
else
{
printf("猜对了\n");
break;
}
d--;
}
}
void menu()
{
printf("1:继续\n");
printf("0:终止\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择\n");
scanf("%d\n", &input);
switch (input)
{
case 1:
game();
break;
case 0:printf("游戏结束了\n");
break;
}
}
while (input);
return 0;