我可以缩短这个if语句吗?

时间:2019-01-10 10:21:53

标签: c++

if语句当前将变量与静态数进行比较,如果它们相等,则为settime7变量提供一个十六进制数,用于将时间发送给单位。

是否可以缩短此if语句:

if(timeM == 0){
   settime7 = 0x00;
}
else if(timeM == 1){
   settime7 = 0x01;
}
else if(timeM == 2){
   settime7 = 0x02;
}
else if(timeM == 3){
   settime7 = 0x03;
}
// ...and so on to timeM == 60 and settime = 0x3C.

2 个答案:

答案 0 :(得分:7)

只需分配变量:

val window = org.apache.spark.sql.expressions.Window.orderBy("hourTimestamp")
val weatherUpdate = df
                    .withColumn("WeatherLag1", lag("Weather", 3).over(window))
                    .withColumn("WeatherLag2", lag("Weather", 6).over(window))
                    .withColumn("WeatherLag3", lag("Weather", 9).over(window))
                    .withColumn("Weather",coalesce($"Weather",$"WeatherLag1",$"WeatherLag2",$"WeatherLag3"))

是否将自己写为十进制或十六进制对于存储的整数都无关紧要。

答案 1 :(得分:3)

如果我很了解你想要

...
else if(timeM == 60){
   settime7 = 0x60;
}

然后:

settime7 = (timeM / 10) * 16 + (timeM % 10)