我正在為一個班級制作一個剪刀石頭布的游戲,我在弄清評分方面遇到了困難。到目前為止,我已經知道了如何更新分數,但卻不能讓它超過1分。我知道我錯過了一些重要的東西,但我對這個有點陌生,所以我不確定該去哪里找。任何建議都將是超級有用的 謝謝你。
這是我的論文代碼:
。 fun paperPressed(view: View) {
val computerChoice = Random.nextInt(2)
paperIcon.setColorFilter(Color.RED)
rockIcon.setColorFilter(Color.BLACK)
ScissorsIcon.setColorFilter(Color.BLACK)。
when (computerChoice){
0 -> aiChoice.text = "他們選擇了巖石。"。
1 -> aiChoice.text = "他們選擇了紙。"。
2 -> aiChoice.text = "他們選擇了剪刀。"。
}
yourChoice.text = "你選擇了紙。"/span>
when (computerChoice) {
0 -> whoWon.text = "你贏了!"
1 -> whoWon.text = "It was a tie!"
2 -> whoWon.text = "你輸了!"。
}
var tie = 0
var win = 0
var lose = 0
平局
贏
失敗
when (computerChoice) {
0 -> winNumber.text = "$win"/span>
1 -> drawNumber.text = "$tie"。
2 -> lossNumber.text = "$lose"。
}
}
uj5u.com熱心網友回復:
答案很簡單,宣告
var tie = 0
var win = 0
var lose = 0
全球范圍內。
現在的情況是,每次你呼叫函式paperPressed 計數器被重置為零,所以全域宣告是一個可取的選擇。
像這樣
private var tie =0
private var winning = 0
private var lose = 0
fun paperPressed(view:View) {
val computerChoice = Random.nextInt(2)
paperIcon.setColorFilter(Color.RED)
rockIcon.setColorFilter(Color.BLACK)
ScissorsIcon.setColorFilter(Color.BLACK)。
when (computerChoice){
0 -> aiChoice.text = "他們選擇了巖石。"。
1 -> aiChoice.text = "他們選擇了紙。"。
2 -> aiChoice.text = "他們選擇了剪刀。"。
}
yourChoice.text = "你選擇了紙。"/span>
when (computerChoice) {
0 -> whoWon.text = "你贏了!"
1 -> whoWon.text = "It was a tie!"
2 -> whoWon.text = "你輸了!"。
}
平局
贏
輸了
when (computerChoice) {
0 -> winNumber.text = "$win"/span>
1 -> drawNumber.text = "$tie"。
2 -> lossNumber.text = "$lose"。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311861.html
標籤:
