猜数字游戏
2016-07-20 本文已影响0人
我忘记了是哪个夏天
// ViewController.swift
// 猜数游戏
//
// Created by apple on 16/7/20.
// Copyright © 2016年 apple. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var result=arc4random_uniform(100)
var text2label=UILabel()
var textfield=UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let titlelabel=UILabel()
titlelabel.frame=CGRectMake(138, 50, 100, 50)
titlelabel.text="猜字游戏"
titlelabel.textAlignment = .Center
self.view.addSubview(titlelabel)
let num1label=UILabel()
num1label.frame=CGRectMake(30, 150, 50, 50)
num1label.text="1"
num1label.textAlignment = .Center
self.view.addSubview(num1label)
super.viewDidLoad()
let textlabel=UILabel()
textlabel.frame=CGRectMake(160, 150, 50, 50)
textlabel.text="到"
textlabel.textAlignment = .Center
self.view.addSubview(textlabel)
let num2label=UILabel()
num2label.frame=CGRectMake(290, 150, 50, 50)
num2label.text="100"
num2label.textAlignment = .Center
self.view.addSubview(num2label)
textfield.frame=CGRectMake(133, 200, 100, 50)
textfield.placeholder="请输入数字"
textfield.keyboardType = .NumberPad
textfield.clearButtonMode=UITextFieldViewMode.Always
textfield.borderStyle = .RoundedRect
self.view.addSubview(textfield)
text2label.frame=CGRectMake(133, 250, 100, 50)
text2label.text="你猜"
text2label.textAlignment = .Center
self.view.addSubview(text2label)
let btn=UIButton()
btn.frame=CGRectMake(133, 300, 100, 100)
let image=UIImage(named:"一次")
btn.setBackgroundImage(image, forState: .Normal)
btn.addTarget(self, action: #selector(z), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
func z(){
let number = UInt32(textfield.text!)
print("结果",result,"输入数",number)
if result<number{
print(text2label.text="太大了")
}
else if result>number{
print(text2label.text="太小了")
}
else{
print(text2label.text="猜对了")
result=arc4random_uniform(100)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}