gpt4 book ai didi

ios - 在测验应用程序中突出显示正确答案

转载 作者:行者123 更新时间:2023-11-30 13:45:14 28 4
gpt4 key购买 nike

我是 iOS 开发新手,并且一直在学习一些在线类(class)。在其中一个项目中,我们开发了一个测验应用程序,我希望通过改进该应用程序来提高我的技能,使其超出类(class)所涵盖的范围。

该应用程序使用 .json 文件作为问题和答案的“数据库”。这个 .json 文件如下所示...

  {
"id" : "1",
"question": "Earth is a:",
"answers": [
"Planet",
"Meteor",
"Star",
"Asteroid"
],
"difficulty": "1"
}

...并且继续回答 500 多个问题。

目前,该应用程序向用户提出一个问题,并有四个可能的答案。在 .json 文件中,第一个答案始终是正确答案,但应用程序经过编码以打乱答案,以便正确答案并不总是首先列出。

该应用程序还经过编码,以便显示答案的四个按钮(我使用四种不同颜色的图像作为按钮)被禁用,并且在用户做出选择后外观也会变暗,但他们选择的按钮不会变暗以便突出他们的选择。

我想要做的是更改此设置,以便突出显示具有正确答案的按钮,作为通知用户正确答案的一种方式。

我使用以下代码来加载问题和答案并随机排列答案:

 func loadAllQuestionsAndAnswers()
{
let path = NSBundle.mainBundle().pathForResource("content", ofType: "json")
let jsonData : NSData = NSData(contentsOfFile: path!)!
allEntries = (try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)) as! NSArray
//println(allEntries)

}


func loadQuestion(index : Int)
{
let entry : NSDictionary = allEntries.objectAtIndex(index) as! NSDictionary
let question : NSString = entry.objectForKey("question") as! NSString
let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray

//println(question)
//println(arr)

labelQuestion.text = question as String

let indices : [Int] = [0,1,2,3]
//let newSequence = shuffle(indices)
let newSequence = indices.shuffle()
var i : Int = 0
for(i = 0; i < newSequence.count; i++)
{
let index = newSequence[i]
if(index == 0)
{
// we need to store the correct answer index
currentCorrectAnswerIndex = i

}

let answer = arr.objectAtIndex(index) as! NSString
switch(i)
{
case 0:
buttonA.setTitle(answer as String, forState: UIControlState.Normal)
break;

case 1:
buttonB.setTitle(answer as String, forState: UIControlState.Normal)
break;

case 2:
buttonC.setTitle(answer as String, forState: UIControlState.Normal)
break;

case 3:
buttonD.setTitle(answer as String, forState: UIControlState.Normal)
break;

default:
break;
}

目前我正在使用此代码来检查答案:

var currentCorrectAnswerIndex : Int = 0



func checkAnswer( answerNumber : Int)
{
if(answerNumber == currentCorrectAnswerIndex)
{
// we have the correct answer
labelFeedback.text = "Correct! +1"
labelFeedback.textColor = UIColor.greenColor()
score = score + 1
labelScore.text = "score: \(score)"
SaveScore()
// later we want to play a "correct" sound effect
PlaySoundCorrect()

}
else
{
// we have the wrong answer
labelFeedback.text = "Wrong answer"
labelFeedback.textColor = UIColor.redColor()
// we want to play a "incorrect" sound effect
PlaySoundWrong()
}

在突出显示按钮方面,我将使用如下编码:

func resetAnswerButtons()
{
buttonA.alpha = 1.0
buttonB.alpha = 1.0
buttonC.alpha = 1.0
buttonD.alpha = 1.0
buttonA.enabled = true
buttonB.enabled = true
buttonC.enabled = true
buttonD.enabled = true
}

@IBAction func PressedButtonA(sender: UIButton) {
print("button A pressed")
buttonB.alpha = 0.3
buttonC.alpha = 0.3
buttonD.alpha = 0.3

buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(0)
}
@IBAction func PressedButtonB(sender: UIButton) {
print("button B pressed")
buttonA.alpha = 0.3
buttonC.alpha = 0.3
buttonD.alpha = 0.3

buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(1)
}

@IBAction func PressedButtonC(sender: UIButton) {
print("button C pressed")
buttonA.alpha = 0.3
buttonB.alpha = 0.3
buttonD.alpha = 0.3

buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(2)
}

@IBAction func PressedButtonD(sender: UIButton) {
print("button D pressed")
buttonA.alpha = 0.3
buttonB.alpha = 0.3
buttonC.alpha = 0.3

buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(3)
}

我无法理解的是如何对应用程序进行编码以突出显示正确的答案?目前,上面的代码通过调暗“其他”按钮的 alpha 值来有效地突出显示按下的按钮。如何让应用程序识别哪个“随机”按钮包含正确答案并突出显示该按钮?

最佳答案

掷骰子时,请记住包含正确答案的按钮的索引。

此外,您可以将所有按钮放入一个数组中,并使用 forall 循环操作所有按钮。这使得代码变得非常非常干净,并且摆脱了奇怪的变量名称和 switch 大小写。

关于ios - 在测验应用程序中突出显示正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com