[英]value of a variable not carried from one state to another in Kotlin
初學者問題 - 知道為什么 SELECT 中的 lemonSize 值在切換檸檬水狀態時沒有傳遞到 SQUEEZE 嗎? 任何幫助是極大的贊賞 :)
該應用程序/代碼是 Google Kotlin 課程介紹的一部分,該課程用於創建一個在屏幕之間旋轉的檸檬水應用程序https://developer.android.com/codelabs/basic-android-kotlin-training-project-lemonade?continue= https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-four%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-project-檸檬水#0
這就是我認為問題所在(我在下面附上了日志):
// Default lemonSize to -1
private var lemonSize = -1
// Default the squeezeCount to -1
private var squeezeCount = -1
private var lemonTree = LemonTree()
when (lemonadeState) {
SELECT -> {
Log.d(TAG, "lemonadeState at beginning of SELECT *should be SELECT* lemonadeState = $lemonadeState")
// Set the lemonSize (the number of squeezes needed) by calling the pick() method
var lemonSize = LemonTree()//UPDATE
Log.d(TAG, "randomly generated lemonSize using LemonTree -> lemonSize = $lemonSize")
// Setting the squeezeCount (the number of times the user has squeezed the lemon) to 0.
squeezeCount = 0
Log.d(TAG, " setting squeezeCount to 0 -> squeezeCount = $squeezeCount")
// Transition to the SQUEEZE state
lemonadeState = SQUEEZE
Log.d(TAG, "lemonadeState at bottom of SELECT *should be SQUEEZE* lemonadeState = $lemonadeState")
}
// TODO: When the image is clicked in the SQUEEZE state the squeezeCount needs to be
// INCREASED by 1 and lemonSize needs to be DECREASED by 1.
// - If the lemonSize has reached 0, it has been juiced and the state should become DRINK
// - Additionally, lemonSize is no longer relevant and should be set to -1
SQUEEZE -> {
Log.d(TAG, "at top of SQUEEZE *lemonSize should be 2-4* lemonSize = $lemonSize")
Log.d(TAG, "at top of SQUEEZE *lemonadeState should be SQUEEZE* lemonadeState = $lemonadeState")
//squeezeCount needs to be INCREASED by 1
squeezeCount += 1
//lemonSize needs to be DECREASED by 1.
Log.d(TAG, "decreasing lemonSize by 1")
Log.d(TAG, "lemonSize before decreasing by 1 lemonSize = $lemonSize")
lemonSize = lemonSize - 1
Log.d(TAG, "lemonSize after decreasing by 1 lemonSize = $lemonSize")
//lemonSize = 0//REMOVE once done with testing
//Log.d(TAG, "lemonSizeHere = $lemonSize ** REMOVE")
//If the lemonSize has reached 0, it has been juiced and the state should become DRINK
if (lemonSize == 0) {
lemonadeState = DRINK
//lemonSize = -1
Log.d(TAG, "lemonadeState when lemonSize == 0 lemonadeState = $lemonadeState")
}
}
/**
* A Lemon tree class with a method to "pick" a lemon. The "size" of the lemon is randomized
* and determines how many times a lemon needs to be squeezed before you get lemonade.
*/
class LemonTree {
fun pick(): Int {
return (2..4).random()
}
}
日志:
2022-06-17 16:20:46.837 8549-8578/com.example.lemonade W/OpenGLRenderer: Failed to initialize 101010-2 format, error = EGL_SUCCESS
2022-06-17 16:20:49.788 8549-8549/com.example.lemonade D/MainActivity: lemonadeState at beginning of SELECT *should be SELECT* lemonadeState = select
2022-06-17 16:20:49.789 8549-8549/com.example.lemonade D/MainActivity: randomly generated lemonSize using LemonTree -> lemonSize = com.example.lemonade.LemonTree@5c31497
2022-06-17 16:20:49.790 8549-8549/com.example.lemonade D/MainActivity: setting squeezeCount to 0 -> squeezeCount = 0
2022-06-17 16:20:49.793 8549-8549/com.example.lemonade D/MainActivity: lemonadeState at bottom of SELECT *should be SQUEEZE* lemonadeState = squeeze
2022-06-17 16:21:04.177 8549-8549/com.example.lemonade D/MainActivity: at top of SQUEEZE *lemonSize should be 2-4* lemonSize = -1
2022-06-17 16:21:04.179 8549-8549/com.example.lemonade D/MainActivity: at top of SQUEEZE *lemonadeState should be SQUEEZE* lemonadeState = squeeze
2022-06-17 16:21:04.184 8549-8549/com.example.lemonade D/MainActivity: lemonSize before decreasing by 1 lemonSize = -1
2022-06-17 16:21:04.186 8549-8549/com.example.lemonade D/MainActivity: lemonSize after decreasing by 1 lemonSize = -2
因此,您正在創建一個名為lemonSize
的頂級變量,該變量已初始化為-1
:
// Default lemonSize to -1
private var lemonSize = -1
該 var 是一個Int
,它是通過您為其分配一個整數值這一事實推斷出來的。 這應該是你現在檸檬的大小,對吧?
但是在您的SELECT
分支中,您正在執行以下操作:
// Set the lemonSize (the number of squeezes needed) by calling the pick() method
var lemonSize = LemonTree()//UPDATE
這里有兩個問題 - 首先,您正在創建一個名為lemonSize
的新變量,它只存在於這些花括號的范圍內,即當您的狀態為SELECT
時執行的代碼。 無論您對該變量做什么,一旦您退出該代碼塊,它就會消失。
因此,當您到達SQUEEZE
分支時,當它指的是lemonSize
時,它正在查看頂級分支,它仍然是-1
。 您在SELECT
中創建的lemonSize
變量已經消失,並且無論如何對SQUEEZE
塊都不可見。
因此,您應該在SELECT
中做的是更改頂級“lemonSize”變量的值(無論如何,這就是您為squeezeCount
和lemonadeState
所做的):
// no 'var' this time, we're setting the top-level one
// THIS LINE WON'T WORK THOUGH! see below
lemonSize = LemonTree()
這就是您遇到其他問題的地方-這不起作用,因為您正在調用LemonTree()
,它是LemonTree
類的構造函數。 您正在創建一個LemonTree
對象並將其分配給lemonSize
變量——正如我們之前看到的,它已被推斷為持有一個Int
類型。 而且LemonTree
不是Int
! 它不能去那里
您當前的代碼運行良好,因為您基本上是在創建一個新的局部變量,它恰好被稱為lemonSize
,並為其分配了一個LemonTree
- 所以您創建了一個具有推斷LemonTree
類型的變量。 而且你不用它做任何事情,所以它從來都不是問題(盡管在你的日志中你可以看到它打印出來很奇怪 - com.example.lemonade.LemonTree@5c31497
意味着它是一個具有參考 ID 的LemonTree
對象,不是一個號碼!)
所以就像那行上面的評論所說,你真的需要調用pick()
來獲取你的lemonSize
。 您可以在此處創建一個新的LemonTree
對象以從中進行選擇——但您已經將一個對象放入頂級變量lemonTree
中。 您可能只是想使用它,對嗎?
最后,您可能想要這樣做:
SELECT -> {
// pick a new current lemon and record its size
lemonSize = lemonTree.pick()
...
}
現在你的頂級lemonSize
狀態變量有了一個新的值,一切都可以看到。
希望這是有道理的! 這里有一些概念需要熟悉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.