我目前正在开发一个项目,该项目使用键盘数字作为传感器 (f_temp) 的保存位置并将其保存到浮点数组 sensorData[] 中,u8_key 是识别按键的变量。如何在不使用多个 else if 语句的情况下改进这一点?
void saveValue(void){
if (u8_key == '1'){
writeLCD(0x01,0,0,1); // clears LCD
sensorData[0] = f_temp;
outStringLCD("Saved to 1"); // writes to LCD for visual confirmation of value being saved and to what location on LCD
}
else if (u8_key == '2'){
writeLCD(0x01,0,0,1);
sensorData[1] = f_temp;
outStringLCD("Saved to 2");
}
else if (u8_key == '3'){
writeLCD(0x01,0,0,1);
sensorData[2] = f_temp;
outStringLCD("Saved to 3");
}
else if (u8_key == '4'){
writeLCD(0x01,0,0,1);
sensorData[3] = f_temp;
outStringLCD("Saved to 4");
}
else if (u8_key == '5'){
writeLCD(0x01,0,0,1);
sensorData[4] = f_temp;
outStringLCD("Saved to 5");
}
else if (u8_key == '6'){
writeLCD(0x01,0,0,1);
sensorData[5] = f_temp;
outStringLCD("Saved to 6");
}
else if (u8_key == '7'){
writeLCD(0x01,0,0,1);
sensorData[6] = f_temp;
outStringLCD("Saved to 7");
}
else if (u8_key == '8'){
writeLCD(0x01,0,0,1);
sensorData[7] = f_temp;
outStringLCD("Saved to 8");
}
else if (u8_key == '9'){
writeLCD(0x01,0,0,1);
sensorData[8] = f_temp;
outStringLCD("Saved to 9");
}
else if (u8_key == '0'){
writeLCD(0x01,0,0,1);
sensorData[9] = f_temp;
outStringLCD("Saved to 0");
}
else if (u8_key == '*'){
writeLCD(0x01,0,0,1);
sensorData[10] = f_temp;
outStringLCD("Saved to *");
}