美文网首页
猜数字游戏

猜数字游戏

作者: 我忘记了是哪个夏天 | 来源:发表于2016-07-20 21:04 被阅读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.
    
    }
    

    }

    相关文章

      网友评论

          本文标题:猜数字游戏

          本文链接:https://www.haomeiwen.com/subject/upavjttx.html