gpt4 book ai didi

java - Android Studio “Variable is never used”,但实际上已使用(BigInteger问题, Unresolved reference )

转载 作者:行者123 更新时间:2023-12-02 13:11:39 27 4
gpt4 key购买 nike

因此,美好的一天eveyone。

我开始通过Android Studio用kotlin编写我自己的第一个应用程序,但有一个我无法解决的问题。写下此代码时:(bi用作BigInteger btw的每个变量的开头。)

if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
}

它告诉我,从未使用过“双向组合”。但是,我实际上在此行中使用它:
val biresult: BigInteger = bicombinations.divide(bipcpower)

在这一行,我对双向组合产生另一个错误/警告,“ Unresolved reference :双向组合”

编辑:第二个警告现在发送至:“变量'bicombinations'必须初始化。” 我知道如何在使用Int或Double或其他类型时解决此问题,但是由于BigIntegers是vals,因此我无法在if语句之外的双向组合上设置值

编辑2:初始化问题的解决方法是:首先,在if分支外部声明双向组合,如下所示:
var bicombinations: BigInteger = ONE

在if分支中,只需使用不带“val”的双组合,就不要再次将其声明为BigInteger。谢谢@ Elliot-frisch

谁能告诉我该怎么办?这真的可以帮助我完成我的应用程序!

最佳答案

在确保设置了变量之前,该变量不在范围内(又名引用),因此您可能需要在所有条件中设置变量,然后才能在if条件之外使用它。

将结果代码移至if分支内

if (length == 2) {
val bicombinations: BigInteger = valueOf(bivariations.toLong())
.multiply(bivariations)
val biresult: BigInteger = bicombinations.divide(bipcpower)
}

或添加一个else分支(如果可以确保设置了 bicombination变量)

val bicombinations: BigInteger
if (length == 2) {
bicombinations = valueOf(bivariations.toLong())
.multiply(bivariations)
} else {
bicombination = BigInteger(0)
}
val biresult: BigInteger = bicombinations.divide(bipcpower)

关于java - Android Studio “Variable is never used”,但实际上已使用(BigInteger问题, Unresolved reference ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com