我分配了以下值。问题在于,在if语句中使用end1和end2值时未分配。 我使用了字符串插值,但仍未分配
我的示例代码是这样的,三个if条件,一个如下所示
if(tp <="1.0")
{
end1="nd"
end2="ms"
}
breakable {
for (row <- df.rdd.collect) {
var ts=row.mkString(",").split(",")(1)
var nd=row.mkString(",").split(",")(2)
var tp=row.mkString(",").split(",")(3)
var ms=row.mkString(",").split(",")(4)
println(end1) // this prints nd
println(end2) // this prints ms
// but in if statement end1 and end2 values doesn't assign with nd and ms
- neither it shows any error nor assigning the values
if( end1 <="0.5" || end2 <="0.5")
endtime=ts
println("end timestamp is" + endtime)
break()
}
}
我希望上面的if语句像
那样执行if( nd <="0.5" || ms <="0.5")
// end1 and end2 changes as per the condition
但此处未分配值,并且确实在if语句内。
答案 0 :(得分:0)
我想这些值只是在if内分配,并且引用在外面时会丢失,因此我建议您先初始化变量,然后将它们传递到任何需要的地方,以使引用不会丢失并且每次更新时都会更新值。