1A2B App
Published in
Dec 1, 2022
目的: 學習產生隨機數字,數字和字串之間的轉換
重點整理:
標題與數字鍵分別拉到各自的Outlet,Connection設定成Outlet Collection
設定每個數字Button的tag欄位
產生不重複的四位數
var answerNumbers = [Int]()
var answerNumber = Int()
for _ in 0...3 {
answerNumber = Int.random(in: 0...9) //亂數產生數字
while answerNumbers.contains(answerNumber) { //當陣列裡沒有重複的數字才停止
answerNumber = Int.random(in: 0...9)
}
answerNumbers.append(answerNumber) //添加數字到陣列裡面
}
判斷AB
var aCount = 0
var bCount = 0
for i in 0...3 {
if inputNumbers[i] == answerNumbers[i] {
aCount += 1
} else if answerNumbers.contains(inputNumbers[i]) {
bCount += 1
}