[英]swift: random numbers in array
我有數字數組
var shoppingList: [String] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"]
我想在我的標簽中顯示來自shoppingList
3個隨機詞。
我也有10個帶有數字1,2,3,4,5,6,7,8,9,0
按鈕。 我想在我的textField
輸入等於數組中3個隨機單詞的數字,如果成功則獲取print("Done")
或獲取print("not")
。 怎么做?
例如:例如我得到這個3個字one , five , three
就意味着我應該加緊進行數字鍵1,5,3
,並在進入該數字textField
。
@IBAction func numerals(_ sender: UIButton) {
let number = sender.currentTitle
textField.text = textField.text! + number!
if (textField.text?.count)! > 2 {
}
}
更新
var rand1 = ""
var rand2 = ""
var rand3 = ""
var textField1 = [Int]()
var code = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var shoppingList: [String] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"]
rand1 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
rand2 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
rand3 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
//ensures rand1 and rand2 are not the same
while(rand2 == rand1){
rand2 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
}
//ensures rand3 is different from rand1 and rand2
while(rand3 == rand1 || rand3 == rand2){
rand3 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
}
code = ["\(rand1), \(rand2), \(rand3)"]
label?.text = "\(rand1), \(rand2), \(rand3)"
}
func wordToNumber(with word: String) -> Int? {
switch word {
case "zero":
return 0
case "one":
return 1
case "two":
return 2
case "three":
return 3
case "four":
return 4
case "five":
return 5
case "six":
return 6
case "seven":
return 7
case "eight":
return 8
case "nine":
return 9
default:
return nil
}
}
func checkIfCodeIsCorrect() -> Bool {
let codeAsNumbers = code.map { return wordToNumber(with: $0) }
print(codeAsNumbers)
return codeAsNumbers == textField1
}
@IBAction func control(_ sender: UIButton) {
let number = sender.currentTitle
textField.text = textField.text! + number!
textField1.append(Int(number!)!)
print(textField1)
if (textField.text?.count)! > 2 {
print(checkIfCodeIsCorrect())
}
}
因此,由於您更改了問題,因此我將再次提交答案。
您需要做的就是簡單地將String
轉換為Int
,以便您可以比較數字。 這是您需要的代碼:
import Foundation
import UIKit
func wordToNumber(with word: String) -> Int? {
switch word {
case "zero":
return 0
case "one":
return 1
case "two":
return 2
case "three":
return 3
case "four":
return 4
case "five":
return 5
case "six":
return 6
case "seven":
return 7
case "eight":
return 8
case "nine":
return 9
default:
return nil
}
}
var textFieldInts = [3, 2, 6, 7, 1]
var code = ["three", "two", "six", "seven", "one"]
func checkIfCodeIsCorrect() -> Bool {
let codeAsNumbers = code.map { return wordToNumber(with: $0) }
return codeAsNumbers == textFieldInts
}
print(checkIfCodeIsCorrect()) // Perform task if they are equal by placing an if or guard statement
textFieldInts
是用戶輸入的值,為[Int]
。
code
是要顯示給用戶輸入的數字,為[String]
。
不確定您要執行的操作是什么,但是您可以這樣做以獲得textField的三個隨機值
@IBAction func numerals(_ sender: UIButton) {
let rand1 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
var rand2 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
var rand3 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
//ensures rand1 and rand2 are not the same
while(rand2 == rand1){
rand2 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
}
//ensures rand3 is different from rand1 and rand2
while(rand3 == rand1 || rand3 == rand2){
rand3 = shoppingList[Int(arc4random()%(UInt32(shoppingList.count)))]
}
let newString = "\(rand1), \(rand2), \(rand3)"
textField.text = newString
}
新問題
關於您評論的新問題,就到這里。 我在Playground中運行了它,它為我工作。 我這樣稱呼它:
let isCorrect = checkIfCodeIsCorrect()
print("isCorrect \(isCorrect)")
這是其他代碼:
var code = ["two","five","seven"]
var textField1 = [2,5,7]
func wordToNumber(with word: String) -> Int{
switch word {
case "zero":
return 0
case "one":
return 1
case "two":
return 2
case "three":
return 3
case "four":
return 4
case "five":
return 5
case "six":
return 6
case "seven":
return 7
case "eight":
return 8
case "nine":
return 9
default:
return -1
}
}
func checkIfCodeIsCorrect() -> Bool {
var codeAsNumbers = [Int]()
for i in 0...code.count-1{
codeAsNumbers.append(wordToNumber(with: code[i]))
}
print(codeAsNumbers)
return codeAsNumbers == textField1
}
顯然,我做了測試用例是因為我沒有實際的代碼。 您將不得不進行一些調整,但這就是這樣做的方式。
一種相當簡單的方法是“隨機”排列數組,然后僅使用前n個元素。
因此,以您的示例為例:
// simple shuffle extension
extension Array {
mutating func shuffle() {
for i in 0 ..< (count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
swapAt(i, j)
}
}
}
// then, inside your function (viewDidLoad, for example)
var shoppingList: [String] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"]
shoppingList.shuffle()
print(shoppingList[0], shoppingList[1], shoppingList[2])
每次運行時,你將有不同的“數字”在索引0,1和2(當然,因為它是一個小樣本,你偶爾會得到相同的價值觀-隨機還挺定義)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.