英文:
Android Studio "Variable is never used", but actually is used (BigInteger Problem, unsolved reference)
问题
好的,以下是翻译好的内容:
所以,大家好。
我开始用 Kotlin 通过 Android Studio 写我的第一个应用程序,但我遇到了一个无法解决的问题。当我写下这段代码时:(顺便提一下,在每个变量前面都使用了 bi
表示它是一个 BigInteger。)
if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
}
它告诉我,“bicombinations” 从未被使用过。然而,实际上我在这一行中使用了它:
val biresult: BigInteger = bicombinations.divide(bipcpower)
在这一行,我得到了关于 “bicombinations” 的另一个错误/警告,“未解析的引用:bicombinations”。
编辑:第二个警告现在变成了:“变量 'bicombinations' 必须被初始化。” 我知道如果使用 Int、Double 或其他类型,我该如何修复这个问题,但由于 BigInteger 是 val,我无法在 if 语句之外为 bicombinations 设置一个值。
编辑 2:初始化问题的解决方法如下: 首先,在 if 分支之外声明 bicombinations,如下所示:
var bicombinations: BigInteger = ONE
在 if 分支内,只需使用 bicombinations,不要在它之前加上 “val”,也不要再次声明它为 BigInteger。感谢 @Elliot-frisch 提供的解决方法。
有谁能告诉我该怎么办?这将帮助我完成我的应用程序!
英文:
So, good day eveyone.
I'm starting to write my very first own app with kotlin via Android Studio, and I have one problem that I cannot solve. When writing down this code: (bi is used infront of every variable that is a BigInteger btw.)
if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
}
it tells me, that "bicombinations" is never used. However, I actually use it in this line here:
val biresult: BigInteger = bicombinations.divide(bipcpower)
At this line, I get another error/warning for bicombinations, "unsolved reference: bicombinations"
EDIT: The second warning now went to: "Variable 'bicombinations' must be initialized." I know how to fix this when using an Int or Double or whatever, but since BigIntegers are vals, I just cannot set a value on bicombinations outside the if statement
EDIT 2: That's how the initialization problem is solved: First, declare bicombinations outside the if branch like so:
var bicombinations: BigInteger = ONE
and inside the if branch, just use bicombinations without the "val" before it, and don't declare it as a BigInteger again. Thanks @Elliot-frisch
Can anyone tell me what to do? This would really help me finishing my app!
答案1
得分: 1
将您的代码更改如下(需要在 if 代码块之外定义变量)
val bicombinations: BigInteger
if (length == 2) {
bicombinations = valueOf(bivariations.toLong())
.multiply(bivariations)
}
英文:
Change your code like below (Need to define your variable outside if block)
val bicombinations: BigInteger
if (length == 2) {
bicombinations = valueOf(bivariations.toLong())
.multiply(bivariations)
}
答案2
得分: 1
直到有一个变量的保证被设置,该变量才在作用域内(也就是在引用中),所以您可能需要在使用它之前在所有条件中设置该变量。
要么将结果代码移到if分支内部
if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
val biresult: BigInteger = bicombinations.divide(bipcpower)
}
要么在if语句中添加一个else分支,以确保变量 bicombination
被设置
val bicombinations: BigInteger
if (length == 2) {
bicombinations = valueOf(bivariations.toLong())
.multiply(bivariations)
} else {
bicombinations = BigInteger(0)
}
val biresult: BigInteger = bicombinations.divide(bipcpower)
英文:
Until there is a guarantee of a variable is set, the variable is not in scope (aka in reference), so you may need to set the variable in all the condition before using it outside the if condition.
Either move the result code inside the if branch
if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
val biresult: BigInteger = bicombinations.divide(bipcpower)
}
or add an else branch to if that ensures the variable bicombination
is set
val bicombinations: BigInteger
if (length == 2) {
bicombinations = valueOf(bivariations.toLong())
.multiply(bivariations)
} else {
bicombination = BigInteger(0)
}
val biresult: BigInteger = bicombinations.divide(bipcpower)
答案3
得分: 1
val biresult = 0.toBigInteger()
这样你就可以初始化 biresult。
当你在 if 语句中定义一个变量时,你不能在外部使用它,否则会出现未解析的引用错误。为了避免这个错误,你应该在函数内部定义它,而不是在外部。
英文:
val biresult=0.toBigInteger()
This way you can initialize biresult.
When you define a variable in if statement, you can't use it out of that and you get unresolved reference error. You should define it in your function not to get this error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论