输入你所猜的整数(假定1-100),与计算机产生的随机数比较,相等则 BOOM,不行等则继续猜,直至找到随机生成的数(铁甲无敌奖门人猜数字,推荐看一下)。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// 随机生成 100 以内的数
srand((unsigned)time(NULL));
int number = rand() % 100, min = 1, max = 100, guess, count = 0;
printf("Input your number[1..100]:");
scanf("%d", &guess);
while(guess > min && guess < max && min < max) {
if (count++ > 6) {
// 最多猜7次
printf("Game Over!");
break;
}
if (guess == number) {
printf("Boom!");
break;
} else if (guess < number) {
min = guess;
} else {
max = guess;
}
printf("Input your number[%d..%d]:", min, max);
scanf("%d", &guess);
}
return 0;
}