import
1 2class ViewController: UIViewController { 3 4 override func viewDidLoad() { 5 super.viewDidLoad() 6 // Do any additional setup after loading the view. 7 } 8 9 10 @IBOutlet weak var answerImageView: UIImageView! 11 12 13 @IBOutlet weak var answerLabel: UILabel! 14 15 var answerNumbert = 0 16 17 18 @IBOutlet weak var answerLabeltwo: UILabel! 19 20 var anserNumber = 0 21 22 @IBAction func shuffleAction(_ sender: Any) { 23 24 var newAnswerNumbert = 0 25 26 repeat { 27 28 newAnswerNumbert = Int.random(in: 0..<11) 29 30 } while answerNumbert == newAnswerNumbert 31 32 answerNumbert = newAnswerNumbert 33 34 35 if answerNumbert == 0 { 36 37 38 39 answerLabel.text = "優しい" 40 answerImageView.image = UIImage(named: "yasasii") 41 42 } else if answerNumbert == 1 { 43 44 answerLabel.text = "馬鹿な" 45 answerImageView.image = UIImage(named: "baka") 46 47 } else if answerNumbert == 2 { 48 49 answerLabel.text = "日本最強の" 50 answerImageView.image = UIImage(named: "saikyou") 51 52 } else if answerNumbert == 3{ 53 54 answerLabel.text = "天才の" 55 answerImageView.image = UIImage(named: "tennsai") 56 57 } else if answerNumbert == 4 { 58 59 answerLabel.text = "かっこいい" 60 answerImageView.image = UIImage(named: "kaltukoii") 61 62 } else if answerNumbert == 5 { 63 64 answerLabel.text = "面白い" 65 answerImageView.image = UIImage(named: "omosiroi") 66 67 } else if answerNumbert == 6 { 68 69 answerLabel.text = "ハイテンションの" 70 answerImageView.image = UIImage(named: "haitennsyonn") 71 72 } else if answerNumbert == 7 { 73 74 answerLabel.text = "汗かきの" 75 answerImageView.image = UIImage(named: "asekaki") 76 77 } else if answerNumbert == 8 { 78 79 answerLabel.text = "足を怪我した" 80 answerImageView.image = UIImage(named: "kega") 81 82 } else if answerNumbert == 9 { 83 84 answerLabel.text = "よく転ける" 85 answerImageView.image = UIImage(named: "kokeru") 86 87 } else if answerNumbert == 10 { 88 89 answerLabel.text = "よく椅子から転げ落ちる" 90 answerImageView.image = UIImage(named: "isu") 91 92 93 //ここ 94 var newAnswerNumber = 0 95 96 repeat { 97 98 newAnswerNumber = Int.random(in: 0..<11) 99 100 } while anserNumber == newAnswerNumber 101 102 anserNumber = newAnswerNumber 103 104 if answerNumbert == 0 { 105 106 answerLabeltwo.text = "サラリーマン" 107 108 } else if anserNumber == 1 { 109 110 answerLabeltwo.text = "大工" 111 112 } else if anserNumber == 2 { 113 114 answerLabeltwo.text = "フランス人" 115 116 } else if anserNumber == 3 { 117 118 answerLabeltwo.text = "日本人" 119 120 } else if anserNumber == 4 { 121 122 answerLabeltwo.text = "歌手" 123 124 } else if anserNumber == 5 { 125 126 answerLabeltwo.text = "アメリカのスパイ" 127 128 } else if anserNumber == 6 { 129 130 answerLabeltwo.text = "ゾンビ" 131 132 } else if anserNumber == 7 { 133 134 answerLabeltwo.text = "宇宙人" 135 136 } else if anserNumber == 8 { 137 138 answerLabeltwo.text = "動物園の飼育員" 139 140 } else if anserNumber == 9 { 141 142 answerLabeltwo.text = "清掃員" 143 144 } else if anserNumber == 10 { 145 146 answerLabeltwo.text = "悪魔" 147 148 } 149 } 150 151 152 153 } 154 } 155 156 157 158 159 160 161コード
ボタンを押すと二つのラベルがランダムに変わるアプリをつくりました。
↑のコードを書きSimulatorを起動するとLabelは正常に変わるのですが、Labeltwoは何回か押さないと変わりません。
解決方法を教えて欲しいです。打ち消し線
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。