[英]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.