-1
我已經從YouTube教程創建了一個測驗應用。現在我想添加一個分數以便玩家查看它。該應用程序的工作原理是:如果選擇選項a將增加3點,如果選擇b則增加2點,如果選擇c則增加1。在測驗應用中創建一個分數
但是每當我更改問題時,得分值都會回到0,我該如何解決?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var answer1: UIButton!
@IBOutlet weak var answer2: UIButton!
@IBOutlet weak var answer3: UIButton!
@IBOutlet weak var nextquestion: UIButton!
@IBOutlet weak var results: UILabel!
var questionnumber = Int()
var total = Int()
override func viewDidLoad() {
super.viewDidLoad()
questionnumber = Int(arc4random_uniform(15))
total = 0
switch questionnumber {
case 0:
Question.text = "Aproximadamente, ¿cuántas veces al día usas el coche?"
answer1.setTitle("3 veces", for: UIControlState.normal)
answer2.setTitle("5 veces", for: UIControlState.normal)
answer3.setTitle("+ 6 veces", for: UIControlState.normal)
break
case 1:
Question.text = "¿Cuánto te tardas bañándote?"
answer1.setTitle("5-10 minutos", for: UIControlState.normal)
answer2.setTitle("10-15 minutos", for: UIControlState.normal)
answer3.setTitle("+ 15 minutos", for: UIControlState.normal)
break
case 2:
Question.text = "¿Separas la basura?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 3:
Question.text = "¿Dejas correr el agua cuando te lavas los dientes?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 4:
Question.text = "¿Lavas tu coche con cubeta o manguera?"
answer1.setTitle("Cubeta", for: UIControlState.normal)
answer2.setTitle("Manguera", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 5:
Question.text = "¿Reciclas o reutilizas el papel?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 6:
Question.text = "¿Usas las bolsas ecológicas en el supermercado?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 7:
Question.text = "¿Pones las pilas en contenedores especiales?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 8:
Question.text = "¿Afinas tu coche cuando te toca?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 9:
Question.text = "¿Reutilizas las botellas de agua?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 10:
Question.text = "¿Qué tan seguido usas la lavadora?"
answer1.setTitle("1-2 veces por semana", for: UIControlState.normal)
answer2.setTitle("3-4 veces por semana", for: UIControlState.normal)
answer3.setTitle("+ 5 veces por semana", for: UIControlState.normal)
break
case 11:
Question.text = "¿Utilizas popote cuando tomas líquidos?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 12:
self.Question.text = "¿Tiras basura en la calle/mar?"
self.answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 13:
Question.text = "¿Utilizas calentador solar en tu casa?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("No", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 14:
Question.text = "¿Dejas luces encendidas cuando no es necesario?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
default:
break
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Add a target to your button
// Create the method you want to call (see target before)
@IBAction func answer1pressed(_ sender: Any) {
total = total + 3
results.text = "Total:\(total)"
}
@IBAction func answer2pressed(_ sender: Any) {
total = total + 2
results.text = "Total:\(total)"
}
@IBAction func answer3pressed(_ sender: Any) {
total = total + 1
results.text = "Total:\(total)"
}
@IBAction func nextquestion(_ sender: Any) {
viewDidLoad()
}
}