選擇題App
Published in
8 min readNov 7, 2020
基本功能:
第一輪,照順序出題,每題有四個選項。
答對一題加 10 分 。
全部有 10 題,畫面上顯示目前題目是第幾題。
進階功能:
可重新開始玩。重新後亂序出題
每題的四個選項的順序不固定。
尚未完成:
計時。
儲存玩家得分的歷史記綠。
將玩家的分數上傳,實現分數排行榜。
搭配線上題庫 API Open Trivia DB。
使用功能:
struct { } 建立資料型別 (與class的差異..還不太清楚QQ)
型別陣列 [型別 ]( )
陣列 [ ]
陣列 的shuffle( )、append( )
button的setTitle( )、currentTitle
Outlet陣列 Outlet Collection
func { }
if { }
\( )
先完成題目陣列 >顯示題目與選項>判斷是否正確>顯示題數與分數>重新開始玩
@IBOutlet weak var questionsText: UITextView!@IBOutlet var choiceButton: [UIButton]!@IBOutlet weak var pointLabel: UILabel!//宣告一個 有題目答案選項 的型別 的陣列
var questionsAnswer = [QuestionsAnswer]() var questions = ["長くならんでいるようす。" , "人にさからったりしないでおとなしいようす。" , "品物を買ったときに払うお金。" , "手で皮膚をかきたくなる感じ。" , "音樂やダンスや会話の速さ。" , "都合が悪くて、日程を変更する。" , "火に焼けて黒くなる。" , "心臓の音がきこえるようす。" , "元気いっぱいのようす。" , "心の底から深く感じるようす。"]var answers = ["ずらり" , "素直" , "代金" , "かゆい" , "テンポ" , "ずらす" , "こげる" , "どきどき" , "いきいき" , "しみじみ"]
var index = 0 //紀錄陣列順序var choiceAnswer = "" //儲存作答按鈕的答案var point = 0 //記錄分數var number = 1 //紀錄題數
super.viewDidLoad()// 生成題目1~10題,並加到題目的陣列裡let question1 = QuestionsAnswer(questions: questions[0], answers: answers[0], choice: ["ずうっと","ゆうゆう","むげん","ずらり"])questionsAnswer.append(question1)let question2 = QuestionsAnswer(questions: questions[1], answers: answers[1], choice: ["正直","純情","素直","真っ直ぐ"])questionsAnswer.append(question2)let question3 = QuestionsAnswer(questions: questions[2], answers: answers[2], choice: ["料金","代金","費用","勘定"])questionsAnswer.append(question3)let question4 = QuestionsAnswer(questions: questions[3], answers: answers[3], choice: ["かたい","ひどい","かゆい","だらしない"])questionsAnswer.append(question4)let question5 = QuestionsAnswer(questions: questions[4], answers: answers[4], choice: ["テンポ","リズム","アクセント","コック"])questionsAnswer.append(question5)let question6 = QuestionsAnswer(questions: questions[5], answers: answers[5], choice: ["よせる","ずらす","はなす","すごす"])questionsAnswer.append(question6)let question7 = QuestionsAnswer(questions: questions[6], answers: answers[6], choice: ["こげる","もえる","かすむ","けむる"])questionsAnswer.append(question7)let question8 = QuestionsAnswer(questions: questions[7], answers: answers[7], choice: ["ふわふわ","どんどん","どきどき","とうとう"])questionsAnswer.append(question8)let question9 = QuestionsAnswer(questions: questions[8], answers: answers[8], choice: ["ふわふわ","いきいき","たびたび","いよいよ"])questionsAnswer.append(question9)let question10 = QuestionsAnswer(questions: questions[9], answers: answers[9], choice: ["はきはき","しばしば","しみじみ","いちいち"])questionsAnswer.append(question10)questionsText.text = questionsAnswer[index].questionspointLabel.text = "第\(number)題,累積\(point)分數"showChoice()
func showChoice() {//把該題目的選項陣列弄亂questionsAnswer[index].choice.shuffle()//設定選項按鈕的標題,根據該題目的選項,迴圈四次for i in 0...3 {choiceButton[i].setTitle(questionsAnswer[index].choice[i], for: .normal)}
@IBAction func answer(_ sender: UIButton) {choiceAnswer = sender.currentTitle!if choiceAnswer == questionsAnswer[index].answers , index < 10 , point < 100 {point = point + 10pointLabel.text = "第\(number)題,累積\(point)分數"}if index < 9 {number = number + 1index = index + 1pointLabel.text = "第\(number)題,累積\(point)分數"questionsText.text = questionsAnswer[index].questionsshowChoice()}}
@IBAction func replay(_ sender: Any) {questionsAnswer.shuffle()index = 0number = 1point = 0pointLabel.text = "第\(number)題,累積\(point)分數"questionsText.text = questionsAnswer[index].questionsshowChoice()