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

Can I shorten this if statement?

本文关键字:语句 if 我可以      更新时间:2023-10-16

这个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.

只需分配变量:

settime7 = timeM;

无论您将整数写为十进制还是十六进制,对于存储的整数都无关紧要。

如果我很清楚你想要的话

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

然后:

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