#実現したいこと
カラーセンサーからの値によって、現在何色なのかを判別するif文を書いているのですが、
条件が多くなってしまい、汚いif文になってしまいました。
きれいに書く方法はありますでしょうか?
colorOutputはenumです。
よろしくお願いします。
#現在のコード
####分岐部分
if(detectedColor.red>=0.2 &&detectedColor.red<0.4 &&detectedColor.green>=0.45 &&detectedColor.blue<0.2){ //yellow actions colorOutput = ColorThing.yellow; }else if(detectedColor.red>=0.3 &&detectedColor.green>=0.2 &&detectedColor.green<0.48 &&detectedColor.blue<0.3){ //red actions colorOutput = ColorThing.red; }else if(detectedColor.red<0.25 &&detectedColor.green>=0.4 &&detectedColor.blue>=0.2 &&detectedColor.blue<0.27){ //green actions colorOutput = ColorThing.green; }else if (detectedColor.red<0.25 &&detectedColor.green>=0.4 &&detectedColor.blue>=0.27){ //blue actions colorOutput = ColorThing.blue; }else { //actions when unreadable colorOutput = ColorThing.cantRead; }
####enum部分
public enum ColorThing { yellow, red, green, blue, cantRead, outOfRange }; ColorThing colorOutput = ColorTHing.cantRead;
回答4件
あなたの回答
tips
プレビュー