//作为没过计算机二级的我,居然自己写对了,尽管还是没希望进黑马,但还是炫耀一个。哈哈:lol
// 疯狂猜数字游戏
// Created by Dast on 15/6/11.
#include <stdio.h>
#include "stdlib.h"
int main(int argc, const char * argv[])
{int guessAgain = 1;
while(guessAgain)
{
//系统随机生成0-999,+1
int num,random1 ;
random1 = arc4random_uniform(1000) + 1;
printf("The answer is :%d,ignore this if u won't cheat!\n",random1);
int flag = 10,count = 1;
// 输入一个数,校验
do
{ printf("input a number 1-1000 please:\n");
scanf("%d",&num);
}while( num < 1 || num > 1000);
if(num == random1)
printf("u guess right!the answer is %d\n",num);//若相等,输出。必须放在下面语句前面
while(num != random1)
{ flag --;//计数
if(flag == 0)
{ printf("u loss!the answer is %d\n",random1);
num = random1;//使while无法继续,所以判定num 和 random1相等的语句必须放在上面,否则也会执行判定相等的语句。
}
else
{ if(num < random1)
{
printf("u still have %d chances,guess bigger:\n",flag);
scanf("%d",&num);
}
else
{ printf("u still have %d chances,guess smaller:\n",flag);
scanf("%d",&num);
}
}
}
//判断是否相等,不等时判定并提示大了或小了,还剩几次机会。重新输入,校验(没做)。循环判定。10次都没猜对输出:你输了
printf("if u want to play again,input 1;");//选择是否继续游戏,此处输入非零即可继续游戏
printf("if u donot want to play,input 0:\n");
scanf("%d",&count);
if(count == 0)
guessAgain = 0;//如输入了0,停止 while(guessAgain) 循环。
}
return 0;
}
|
|