// // main.c // 应用-疯狂猜数游戏 // // Created by black on 15/10/24. // Copyright © 2015年 black. All rights reserved. //
#include <stdio.h> #include <stdlib.h>
int main(int argc, const char * argv[]) {
//定义一个计数器,初始值为10 int count = 10;
//获取随机数 int num = arc4random_uniform(1000);
//定义一个用户输入的值 int player = 0;
//界面提示 printf("欢迎来到疯狂猜数游戏!\n"); printf("目前你还有%d次机会!请猜数:(1~1000之间)\n",count); //获取player scanf("%d",&player);
//while循环,次数达到十次或者player == num是结束循环 while (player != num && count > 1) {
//player < num 时,提示并count-1 if (player < num) { count--; printf("猜小了! 请继续:\n"); printf("目前你还有%d次机会\n",count); scanf("%d",&player); } //player > num 时,提示并count-1 if (player > num) { count--; printf("猜大了! 请继续:\n"); printf("目前你还有%d次机会\n",count); scanf("%d",&player); }
}
//循环结束后,判断是那种原因导致 //num == player 提示并打印num验证 if (num == player) { printf("恭喜你!猜对了!\n"); printf("num = %d\n",num); }
//否则,提示机会用完,并打印num验证 else { printf("很遗憾,你的机会用完了。\n"); printf("num = %d\n",num); }
return 0; }
|