Home>

I'm a beginner about a month after I started programming. I am making a rock-paper-scissors app while looking at a book. I used the repeat function to randomly display the results of rock-paper-scissors. At that time, I was wondering, but when I tried to display the rock-paper-scissors result randomly with the following code, I thought that Goo would not appear at the beginning.
Since 0 is assigned to answerNumber at the beginning, I don't think that the value 0 will be entered in newAnsweNumber when passing through the first repeat function. How is it? Thanks for your answer.

I will put the code.
@IBOutlet weak var answerLabel: UILabel!

var answerNumber = 0

@IBAction func startGame (_ sender: Any) {

var newAnswerNumber = 0

repeat {

newAnswerNumber = Int.random (in: 0 ..<3)

} while answerNumber == newAnswerNumber

answerNumber = newAnswerNumber

if answerNumber == 0 {
answerLabel.text = "Goo"
answerImage.image = UIImage (named: "gu")

} else if answerNumber == 1 {
answerLabel.text = "Choki"
answerImage.image = UIImage (named: "choki")

} else if answerNumber == 2 {
answerLabel.text = "par"
answerImage.image = UIImage (named: "pa")
}

}

  • Answer # 1

    It's hard to say because I don't know the whole code,

      var answerNumber = -1

    And the initial value0 --2I think there is one way to set it to a value other than.