我在实现此条件语句时遇到问题
我有3个字符串数据B,R,G
B =蓝色
R = RED
G =绿色
现在我必须满足这个陈述
如果符合此,我将在控制台中显示"Blue"
字符串。第二个声明是,这也必须在控制台中显示"Blue"
字符串,其余条件为"Red"
。
注意:请注意绿色。
我该怎么做?
到目前为止我试过这个
if (a != null && b!= null && c!= null && d!= null)
{
//red string data in console
}
else if (a!= null && b!= null && c== null && d== null)
{
//red string
}
else
{
//blue string
}
但如果他们没有像RED,BLUE,BLUE,RED这样的例子的绿色,那么只要它没有Red
,我就会console
字符串值绿色值
答案 0 :(得分:-1)
问题对我来说有点混乱,但是如果我做对了,你会得到4个输入和条件块,分为2列,类似于你的帖子附带的第一张图片。
第一块: 绿色 绿色
第二块: 绿色 蓝色
或者
第一块: 绿色 绿色
第二块: 绿色 红色
所以翻译成伪代码将是:
//I'm avoiding type classes, if the color is an Enum, or booleans, just typing the idea, if you provide me more information I can "clean" the code
firstInput;
secondInput;
thirdInput;
fourthInput;
//First Statement
firstBlock = ((firstInput == green) && (secondInput == green));
secondBlock = ((thirdInput == green) && (fourthInput == blue)) || ((thirdInput == green) && (fourthInput == red));
if(firstBlock && secondBlock){
print("blue string");
}
//Second Statement
firstBlock = ((firstInput == blue) && (secondInput == green));
secondBlock = ((thirdInput == red) && (fourthInput == red));
if(firstBlock && secondBlock){
print("blue string");
}
else{
print("red string");
}
//So we can transform it to:
firstBlock = ((firstInput == green) && (secondInput == green));
secondBlock = ((thirdInput == green) && (fourthInput == blue)) || ((thirdInput == green) && (fourthInput == red));
firstStatement = firstBlock && secondBlock;
firstBlock = ((firstInput == blue) && (secondInput == green));
secondBlock = ((thirdInput == red) && (fourthInput == red));
secondStatement = firstBlock && secondBlock;
if(firstStatement || secondStatement){
print("blue string")
}
else{
print("red string")
}
绝对不是最简单和更好的解决方案,但如果您向我提供更多信息/反馈,我们可以努力改进它。