简体   繁体   English

Android Studio “从未使用过变量”,但实际使用过(BigInteger 问题,未解决的参考资料)

[英]Android Studio “Variable is never used”, but actually is used (BigInteger Problem, unsolved reference)

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.我开始通过 Android Studio 使用 kotlin 编写我自己的第一个应用程序,但我有一个无法解决的问题。 When writing down this code: (bi is used infront of every variable that is a BigInteger btw.)写下这段代码时: (bi 用于 BigInteger 顺便说一句的每个变量的前面。)

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"在这一行,我收到另一个关于 bicombinations 的错误/警告,“未解决的参考: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我知道在使用 Int 或 Double 或其他什么时如何解决这个问题,但由于 BigIntegers 是 val,我无法在 if 语句之外的 bicombinations 上设置值

EDIT 2: That's how the initialization problem is solved: First, declare bicombinations outside the if branch like so:编辑 2:这就是初始化问题的解决方法:首先,在 if 分支之外声明 bicombinations,如下所示:

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.在 if 分支中,只需使用前面没有“val”的双组合,并且不要再次将其声明为 BigInteger。 Thanks @Elliot-frisch谢谢@Elliot-frisch

Can anyone tell me what to do?谁能告诉我该怎么做? This would really help me finishing my app!这真的会帮助我完成我的应用程序!

Change your code like below (Need to define your variable outside if block)如下更改您的代码(需要在 if 块之外定义您的变量)

val bicombinations: BigInteger

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

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.在保证设置了变量之前,该变量不在 scope 中(也称为参考),因此您可能需要在所有条件下设置变量,然后才能在 if 条件之外使用它。

Either move the result code inside the if branch将结果代码移动到 if 分支内

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或添加一个 else 分支到 if 以确保设置了变量bicombination

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

This way you can initialize biresult.这样你就可以初始化 biresult。 When you define a variable in if statement, you can't use it out of that and you get unresolved reference error.当你在 if 语句中定义一个变量时,你不能使用它,你会得到未解决的引用错误。 You should define it in your function not to get this error.您应该在 function 中定义它,以免出现此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM