.hhhhhhh
{
UITextField* _textField;
UILabel* _label;
int _number;//生成的你要猜的数字
int _count;//猜错的次数(5次)
}
.mmmmm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//背景
UIImageView* imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"8.jpg"];
[self.window addSubview:imageView];
//输入框(你要猜的数字)
_textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 40, 160, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"请输入1~99的数字";
_textField.keyboardType = UIKeyboardTypeNumberPad;//定义该输入框的键盘为数字键盘
_textField.clearsOnBeginEditing = YES;//再次输入前要清空输入框
//变为第一响应(让键盘弹出来)
//[_textField becomeFirstResponder];
[self.window addSubview:_textField];
//button
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(210, 40, 80, 30);
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"提交" forState:UIControlStateNormal];
[button setTitle:@"提交……" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
//UILabel
_label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 260, 80)];
_label.backgroundColor = [UIColor lightGrayColor];
_label.text = @"欢迎来到猜数字游戏";
_label.textColor = [UIColor yellowColor];
_label.textAlignment = NSTextAlignmentCenter;//设置文字居中
_label.font = [UIFont boldSystemFontOfSize:28];//设置字体大小
[self.window addSubview:_label];
//生成要猜的数字:1~99
_number = arc4random() % 99 + 1;
NSLog(@"%d",_number);
return YES;
}
#pragma mark button的点击事件
- (void)buttonClick:(UIButton* )btn
{
_count ++;
//获取你猜的输入框的数字
//intValue 把字符串转化为int类型
int guess_number = [_textField.text intValue];
//判断数字是不是:1~99
if (guess_number >=1 && guess_number <= 99)
{
// _count ++;
if (_count > 5)
{
_label.text =@"游戏结束,新的数字生成" ;
//return;
_number = arc4random() % 99 + 1;
NSLog(@"%d",_number);
_count=0;
}
//比较
if (guess_number > _number)
{
_label.text = @"不好意思,您猜大了";
if (_count == 5) {
_label.text = @"游戏结束,新的数字生成";
_number = arc4random() % 99 + 1;
NSLog(@"%d",_number);
_count=0;
}
}else if (guess_number < _number)
{
_label.text = @"不好意思,您猜小了";
if (_count == 5) {
_label.text = @"游戏结束,新的数字生成";
_number = arc4random() % 99 + 1;
NSLog(@"%d",_number);
_count=0;
}
}else
{
_label.text = @"恭喜您,猜对了";
_number = arc4random() % 99 + 1;
NSLog(@"%d",_number);
_count=0;
}
}else
{
_label.text = @"请看清要求再来……";
}
//输入框内容清空
_textField.text = @"";
} |
|