#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int target_num = 0; int answer_num = 0; int count = 0; char select = 'y'; srand(time(NULL)); target_num = rand() % 100; while(select == 'y' || select == 'Y') { printf("有一个100以内的数,先判断它是奇数还是偶数。1、奇数 2、偶数 "); while(1) { fflush(stdin); select = getchar(); if(target_num % 2 == 1 && select == '1') { printf("没错!就是奇数!接下来具体猜出这个数字吧! "); break; } else if(target_num % 2 == 0 && select == '2') { printf("对!就是偶数!接下来具体猜出这个数字把!\n"); break; } else { printf("猜错啦,再试试看! "); } } printf("输入你的第一次猜测,按回车键结束。只有五次机会哦~\n"); while(1) { scanf("%d", &answer_num); if(count < 5) { if(answer_num < target_num) { printf("很遗憾~低了! "); } else if(answer_num > target_num) { printf("很遗憾~高了! "); } else { printf("太厉害了~猜对咯!答案就是%d\n", target_num); break; } } else { printf("对不起,五次机会已经没有,你输了!"); break; } } printf("还要继续吗?按Y继续游戏,按N退出游戏 "); fflush(stdin); select = getchar(); } return 0; }