迅速:数组中的随机数

时间:2018-07-18 16:46:27

标签: ios swift

我有数字数组

mapper resource="mappers/User.xml"

我想在标签中显示var shoppingList: [String] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"] 中的3个随机词。

我还有10个带有数字shoppingList的按钮。我想在我的1,2,3,4,5,6,7,8,9,0中输入等于数组中3个随机单词的数字,如果成功则获得textField或得到print("Done")。怎么做?

示例:例如,我得到这3个单词print("not"),这意味着我应该按下带有数字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 {             
    }        
}

3 个答案:

答案 0 :(得分:2)

因此,由于您更改了问题,我将再次提交答案。

您需要做的就是简单地将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]

答案 1 :(得分:1)

不确定要执行的操作是什么,但是您可以这样做以获得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
}

很显然,我制作了测试用例,因为我没有实际的代码。您将不得不进行一些调整,但这就是这样做的方式。

答案 2 :(得分:1)

一个相当简单的方法是“随机”排列数组,然后仅使用前 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都会有不同的“数字”(好吧,因为这是一个小样本,所以 有时会得到相同的值-有点的定义)。