gpt4 book ai didi

ios - Swift 如何从两个文本字段中获取 Int 并生成随机数

转载 作者:行者123 更新时间:2023-11-30 13:23:41 25 4
gpt4 key购买 nike

我必须在不同的场景上输入文本字段。一个文本字段的数字较小,另一个文本字段的数字较大。我需要获取这些数字并使用最小的数字和最大的数字生成一个随机数。然后我需要解决乘法问题。示例:用户输入 4 和 8。我需要生成两个大于 4 且小于 8 的随机数。这是我的代码:

//
// CustomModeWithTimer.swift
// BetaVersion0.1
//
// Created by James Ikeler on 5/26/16.
// Copyright © 2016 James Ikeler. All rights reserved.
//

import UIKit
import Foundation


var CustomNumber1 = UInt32(CustomInputForGame)
var CustomNumber2 = UInt32(CustomInput2ForGame)
var Random = arc4random_uniform(CustomNumber2) + CustomNumber1
var Random2 = arc4random_uniform(CustomNumber2) + CustomNumber1
var UnConvertInt = 0
var scorecustom = 0
var TimerCustom = NSTimer()
var CountDownCustom = 10
var GameOverCustom = UIAlertView()
var AnswerCustom = Int(CustomNumber1 * CustomNumber2)


class CustomModeWithTimer: UIViewController {

@IBOutlet weak var CustomInputForGame3: UITextField!
@IBOutlet weak var QuestionForCustomGame: UILabel!
@IBOutlet weak var ScoreLabelForCustom: UILabel!
@IBOutlet weak var TimerCustomLabel: UILabel!
@IBOutlet weak var RightOrWrongCustom: UILabel!

@IBOutlet weak var CustomImgForGame: UIImageView!

func IfAnswerWrong() {
print("TEST\(AnswerCustom)")
CustomInputForGame3.text = ""
CustomImgForGame.image = UIImage(named: "Label")
CustomImgForGame.hidden = false
RightOrWrongCustom.hidden = false
RightOrWrongCustom.text = "Wrong!"
RightOrWrongCustom.textColor = UIColor(red: 225, green: 0, blue: 0, alpha: 1)

}


func IfAnswerRight() {
CountDownCustom = 10
scorecustom += 1
ScoreLabelForCustom.text = "Score: \(scorecustom)"
Random = arc4random_uniform(CustomNumber2) + CustomNumber1
Random2 = arc4random_uniform(CustomNumber2) + CustomNumber1

QuestionForCustomGame.text = "\(Random) x \(Random2)"
AnswerCustom = Int(Random * Random2)
CustomImgForGame.image = UIImage(named: "Label")
RightOrWrongCustom.hidden = false
CustomImgForGame.hidden = false
RightOrWrongCustom.text = "Right!"
RightOrWrongCustom.textColor = UIColor(red: 0, green: 225, blue: 0, alpha: 1)
CustomInputForGame3.text = ""


}

@IBAction func ConfirmAnswerCustom(sender: AnyObject) {
TimerCustom.invalidate()
TimerCustom = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self,selector: Selector("UpdateClockCustom"), userInfo: nil, repeats: true)
let InputAnswerCustom = Int(CustomInputForGame3.text!)
if InputAnswerCustom == AnswerCustom {
IfAnswerRight();
} else {
IfAnswerWrong();

}
}

func UpdateClockCustom() {
TimerCustomLabel.text = ("Time: \(CountDownCustom--)")
if CountDownCustom == -2 {
TimerCustom.invalidate()
GameOverCustom.title = "Game Over!"
GameOverCustom.message = "Nice job you got a score of \(scorecustom).The answer to the question you missed was \(AnswerCustom)!"
GameOverCustom.addButtonWithTitle("Play Again!")
GameOverCustom.show()
}
}
override func viewDidLoad() {
super.viewDidLoad()


// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

最佳答案

定义 randBetween功能:

func randBetween(lower: Int, _ upper: Int) -> Int {
return Int(arc4random_uniform(UInt32(upper - lower - 1))) + lower + 1
}

这将返回一个随机数 x哪里lower < x < upper 。它不会检查 lower + 1 < upper您可以决定如何处理自己。

关于ios - Swift 如何从两个文本字段中获取 Int 并生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com