猜数字游戏
计算机随机产生一个1~100之间的数,让用户来请。如果猜对了给出提示“Wonderful, you are right!", 如果猜错了就提示“Sorry, you are wrong.",并告诉用户是猜大了(too high)还是猜小了(too low)。最多只能猜8次。
实现程序代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int number, guess, i = 8;
srand((unsigned int)time(NULL));
number = rand() % 100 + 1;
printf("Please guess a number:\n");
while(i > 0) {
scanf("%d", &guess);
if (guess == number) {
printf("Wonderful, you are right!");
return 0;
} else if (guess > number) {
printf("Sorry, you are wrong.");
printf("too high\n");
} else {
printf("Sorry, you are wrong.");
printf("too low\n");
}
i--;
}
printf("Game over!\n\n");
return 0;
}
|
阅读(378) | 评论(0) | 转发(0) |