如果我想跟踪我的分数,我正在尝试创建一个测验应用程序。
但是,问题的分数都有一些不同的价值。我希望分数加上前面的值。
到目前为止,这是我的代码。我期待着你的回答。非常感谢。
(我对结构和问题有单独的看法(
当你看到我的代码时,如果你看到一些可以改进的地方,请让我知道
(我知道我不知道,但很想学习(
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var storyLabel: UILabel!
@IBOutlet weak var choice1Button: UIButton!
@IBOutlet weak var choice2Button: UIButton!
@IBOutlet weak var scoreLabel: UILabel!
var storyBrain = StoryBrain()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
@IBAction func choiceMade(_ sender: UIButton) {
storyBrain.nextStory(userChoice: sender.currentTitle!)
storyBrain.ScoreUP(userChoice: sender.currentTitle!)
updateUI()
}
func updateUI() {
storyLabel.text = storyBrain.getStoryTitle()
scoreLabel.text = String(Int(storyBrain.score))
choice2Button.setTitle(storyBrain.getChoice2(), for: .normal)
choice1Button.setTitle(storyBrain.getChoice1(), for: .normal)
//if (choice1Button != nil) {
// var score = "(storyBrain.GetScoreX1())"
// scoreLabel.text = score
//} else if (choice2Button != nil) {
// var score = "(storyBrain.score + storyBrain.GetScoreX2())"
//scoreLabel.text = score
}
}
import Foundation
struct Story {
let title: String
let choice1: String
let choice1Destination: Int
let choice2: String
let choice2Destination: Int
let scoreX1: Double
let scoreX2: Double
}
import Foundation
import UIKit
struct StoryBrain {
var score = 0
var ScoreTTT = 0
var storyNumber = 0
let stories = [
Story( //0
title: “Q1”,
choice1: “XXX”, choice1Destination: 1,
choice2: “XXX” , choice2Destination: 1,
scoreX1: 5, scoreX2: 3
),
Story( //1
title: “Q2”,
choice1: “XXX”, choice1Destination: 2,
choice2: "XXX", choice2Destination: 2,
scoreX1: 2, scoreX2: 7
),
Story( //2
title: “Q3”,
choice1: "XXX", choice1Destination: 3,
choice2: "XXX", choice2Destination: 3,
scoreX1: 10, scoreX2: 12
),
Story( //3
title: “Q4”,
choice1: "XXX", choice1Destination: 4,
choice2: "XXX", choice2Destination: 4,
scoreX1: 10, scoreX2: -15
),
Story( //4
title: “Q”5,
choice1: "XXX", choice1Destination: 5,
choice2: "XXX", choice2Destination: 5,
scoreX1: 10, scoreX2: -15
///there is approximatively 50Q
)
]
func getProgress() -> Float { // For the progress bar
return Float(storyNumber) / Float(stories.count)
}
func getStoryTitle() -> String {
return stories[storyNumber].title
}
func getChoice1() -> String {
return stories[storyNumber].choice1
}
func getChoice2() -> String {
return stories[storyNumber].choice2
}
mutating func nextStory(userChoice: String) {
let currentStory = stories[storyNumber]
if userChoice == currentStory.choice1 {
storyNumber = currentStory.choice1Destination
} else if userChoice == currentStory.choice2 {
storyNumber = currentStory.choice2Destination
}
}
//TEST FUNCTION FOR SCORE
func GetScoreX1() -> Double {
return stories[storyNumber].scoreX1
}
func GetScoreX2() -> Double {
return stories[storyNumber].scoreX2
}
mutating func ScoreUP(userChoice: String) -> Int {
let currentStory = stories[storyNumber]
if userChoice == currentStory.choice1 {
score = Int(Double(currentStory.scoreX1))
} else if userChoice == currentStory.choice2 {
score = Int(Double(currentStory.scoreX2))
}
return score
}
}
在变异函数ScoreUP中,对得分变量进行以下更新
if userChoice == currentStory.choice1 {
score += Int(Double(currentStory.scoreX1))
} else if userChoice == currentStory.choice2 {
score += Int(Double(currentStory.scoreX2))
}