0
現在,我的問題是,當我試圖計算得分線時,如果某人在文本字段中寫入與「42」不相同的內容(這與label.text相同)他們將受到1減去1點的懲罰。如果我寫了一些不是「42」的東西,它給了我1個減號點,但只是第一次。如果我再做一次,比分線不會改變,所以我會卡在-1積分線上。用swift計算得分
我怎麼能這樣做,每次我寫的東西不是「42」,它會給我一個-1點,即使我已經有-1分?
(正如你可以看到我已經設置了label.text是一樣的TextField.text如果你想知道)
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: PROBERTIES
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label1: UILabel!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
imagePicker.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textfield: UITextField) {
label.text = textField.text
var score = 0
if textField.text != "42" {
score -= 1
}
// MARK: if-statements
if label.text! == "42" {
label1.text = "You are correct"
self.view.backgroundColor = UIColor.greenColor()
score += 10
}
if label.text != "42" {
label1.text = "Damn, wrong"
self.view.backgroundColor = UIColor.redColor()
}
if label.text!.isEmpty {
label1.text = "Still waiting for an answer..."
}
if label.text == "happiness" {
label1.text = "It is not a key element in the meaning of life"
}
if label.text == "to be happy" {
label1.text = "i would rather be rich"
}
if label.text == "money" {
label1.text = "there is more in this world than money"
}
if label.text == "There is no meaning of life" {
label1.text = "There is a meaning to everything"
}
if label.text == "there is no meaning of life"
{
label1.text = "there is a meaning to everything"
}
if label.text == "to be rich" {
label1.text = "Money does not solve every problem"
}
var scoreString = String(score)
scoreLabel.text = "Your score is \(scoreString)"
}
嗨你究竟是什麼意思?「我怎麼能這樣做,每次我寫的東西不是」42「,它會給我一個-1點,即使我已經有-1分?」,例如你有100分,而不是打字42會給你99分,如果你不再輸入42,那麼它將保持在99?還是98? – StealthOne
我的意思是我只能觸發「-1」(如果我寫的東西不是「42」)和「+10」(如果我寫「42」)一次。例如,我從0開始寫一些不等於42的東西,然後結束文本字段編輯,我的scoreLabel更改爲「Your score -1」,但是當我開始再次編輯並寫入不等於「42 「那麼我的scoreLabel仍然會看起來一樣」你的分數-1「 –