dimanche 21 juin 2020

Swift, How to change type from string?

I wrote the code to got 100 by random math symbols an numbers from 1 to 9 but I can't change order of numbers, its giving me an error that I can't assign value of type 'String' to type 'Int' on line 18 what I can do now??

        let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    let arr1 = ["+", "-", "*"]
     
    for _ in 1...100{
        var x = 0
        if x == 100{
            print(x)
        }else{
            let a1 = arr[0]
            let a2 = arr[1]
            let a3 = arr[2]
            let a4 = arr[3]
            let a5 = arr[4]
            let a6 = arr[5]
            let a7 = arr[6]
            let a8 = arr[7]
            let a9 = arr[8]
            x = "\(a1) \(arr1.randomElement()) \(a2) \(arr1.randomElement()) \(a3) \(arr1.randomElement()) \(a4) \(arr1.randomElement()) \(a5) \(arr1.randomElement()) \(a6) \(arr1.randomElement()) \(a7) \(arr1.randomElement()) \(a8) \(arr1.randomElement()) \(a9)"
            print("symbol is \(x)")
        }
    }



Aucun commentaire:

Enregistrer un commentaire