遇到此问题时我正在编码,我想知道这是否正确或出于性能原因可以进行改进:
if (color == 0) {
if (c.val[0] >= 0 && c.val[0] <= 64) {
//Black
Paint(cursor.x + x, cursor.y + y);
}
}
else if (color == 1) {
if (c.val[0] >= 64 && c.val[0] <= 128) {
//Grey
Paint(cursor.x + x, cursor.y + y);
}
}
else if (color == 2) {
if (c.val[0] >= 128 && c.val[0] <= 192) {
//White Gray
Paint(cursor.x + x, cursor.y + y);
}
}
如您所见,即时消息在所有IF语句中执行的操作完全相同,而且看起来有点怪异,该程序可以按我的要求工作,但我确实认为即时消息缺少某些内容。
谢谢!
最佳答案
一些逻辑运算使其更紧凑。
if ((color == 0 && c.val[0] >= 0 && c.val[0] <= 64) ||
(color == 1 && c.val[0] >= 64 && c.val[0] <= 128) ||
(color == 2 && c.val[0] >= 128 && c.val[0] <= 192)) {
Paint(cursor.x + x, cursor.y + y);
}
关于c++ - 可以在各种 'if'语句中做同样的事情吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54372606/