Swift 剪刀开关
我想做一个简单的石头剪刀游戏。为此,我使用了一个switch语句,但由于某些原因,它不能正常工作 这就是我制作这个小游戏的结构: 石头、剪刀、布有三个按钮,你必须选择一个。 有一个标签告诉对手(计算机)选择了什么,我把它命名为opponentLabel。 有一个标签告诉结果是什么(“例如,你赢了”),我将其命名为resultLabel 它是这样工作的(这就是它的结构): 然后我写了一个switch语句,把它们放在一起。 问题是,出于某种原因,当我运行应用程序时,如果我选择摇滚乐,一切都很好,但当我选择纸或剪刀时,结果是错误的 例如,如果我选择了纸(a=1),而对手有纸(这意味着随机数恰好是随机数=1),结果标签不是应该的“画”,而是“你输了”:纸和剪刀不起作用!!我做错了什么? 以下是完整的代码:Swift 剪刀开关,swift,switch-statement,Swift,Switch Statement,我想做一个简单的石头剪刀游戏。为此,我使用了一个switch语句,但由于某些原因,它不能正常工作 这就是我制作这个小游戏的结构: 石头、剪刀、布有三个按钮,你必须选择一个。 有一个标签告诉对手(计算机)选择了什么,我把它命名为opponentLabel。 有一个标签告诉结果是什么(“例如,你赢了”),我将其命名为resultLabel 它是这样工作的(这就是它的结构): 然后我写了一个switch语句,把它们放在一起。 问题是,出于某种原因,当我运行应用程序时,如果我选择摇滚乐,一切都很好,但当
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var opponentLabel: UILabel!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var rockButton: UIButton!
@IBOutlet weak var paperButton: UIButton!
@IBOutlet weak var scissorsButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Hide() {
opponentLabel.hidden = true
resultLabel.hidden = true
}
func unHide() {
opponentLabel.hidden = false
resultLabel.hidden = false
}
var a = Int()
var randomNumber = Int()
func randomChoice() {
randomNumber = Int(arc4random() % 3)
NSLog("randomNumber%ld", randomNumber)
}
func gameOn() {
switch(randomNumber) {
case 0:
opponentLabel.text = "The opponent chose : ROCK"
if a == 0 {
resultLabel.text = "DRAW"
} else {
if a == 1 {
resultLabel.text = "YOU WON!"
}
if a == 2 {
resultLabel.text = "YOU LOST!"
}
}
unHide()
break
case 1:
opponentLabel.text = "The opponent chose: PAPER"
if a == 0 {
resultLabel.text = "YOU LOST!"
} else {
if a == 1 {
resultLabel.text = "DRAW"
}
if a == 2 {
resultLabel.text = "YOU WON!"
}
}
unHide()
break
case 2:
opponentLabel.text = "The opponent chose: SCISSORS"
if a == 0 {
resultLabel.text = "YOU WON!"
} else {
if a == 1 {
resultLabel.text = "YOU LOST!"
}
if a == 2 {
resultLabel.text = "DRAW"
}
}
unHide()
break
default:
break
}
}
@IBAction func rockButton(sender: AnyObject) {
a == 0
randomChoice()
gameOn()
}
@IBAction func paperButton(sender: AnyObject) {
a == 1
randomChoice()
gameOn()
}
@IBAction func scissorsButton(sender: AnyObject) {
a == 2
randomChoice()
gameOn()
}
}
在@iAction函数中,您将“a”与0、1或2进行比较,而不是给“a”该值
更改代码,使其不是a==0,而是a=0。对3个@iBaction执行此操作,然后再试一次,它应该可以工作。谢谢@AEkon!!它奏效了,而且。。我现在学到了一些新东西!您应该避免使用break语句,默认情况除外(因为其中没有任何操作)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var opponentLabel: UILabel!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var rockButton: UIButton!
@IBOutlet weak var paperButton: UIButton!
@IBOutlet weak var scissorsButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Hide() {
opponentLabel.hidden = true
resultLabel.hidden = true
}
func unHide() {
opponentLabel.hidden = false
resultLabel.hidden = false
}
var a = Int()
var randomNumber = Int()
func randomChoice() {
randomNumber = Int(arc4random() % 3)
NSLog("randomNumber%ld", randomNumber)
}
func gameOn() {
switch(randomNumber) {
case 0:
opponentLabel.text = "The opponent chose : ROCK"
if a == 0 {
resultLabel.text = "DRAW"
} else {
if a == 1 {
resultLabel.text = "YOU WON!"
}
if a == 2 {
resultLabel.text = "YOU LOST!"
}
}
unHide()
break
case 1:
opponentLabel.text = "The opponent chose: PAPER"
if a == 0 {
resultLabel.text = "YOU LOST!"
} else {
if a == 1 {
resultLabel.text = "DRAW"
}
if a == 2 {
resultLabel.text = "YOU WON!"
}
}
unHide()
break
case 2:
opponentLabel.text = "The opponent chose: SCISSORS"
if a == 0 {
resultLabel.text = "YOU WON!"
} else {
if a == 1 {
resultLabel.text = "YOU LOST!"
}
if a == 2 {
resultLabel.text = "DRAW"
}
}
unHide()
break
default:
break
}
}
@IBAction func rockButton(sender: AnyObject) {
a == 0
randomChoice()
gameOn()
}
@IBAction func paperButton(sender: AnyObject) {
a == 1
randomChoice()
gameOn()
}
@IBAction func scissorsButton(sender: AnyObject) {
a == 2
randomChoice()
gameOn()
}
}