我正在做家庭作业,我们应该向用户询问2种原色(红色,蓝色和黄色)。它们不能是数值,它们必须是文字。然后它应该采用两种颜色和"混合"它们在一起产生最终的颜色。我在下面发布的代码有效,但我想知道是否有更有效的方法来做到这一点因为我可以看到这种方式非常有问题,如果一个人必须将两种以上的颜色组合在一起。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string color1="", color2="";
try
{
cout << "Please input your first color" << endl;
getline(cin, color1);
if (color1 != "red" || "yellow" || "blue"){
throw "Invalid Color";
}
}
catch (string e)
{
cout << e << endl;
system("pause");
return -1;
}
try
{
cout << "Please input your second color" << endl;
getline(cin, color2);
if (color2 != "red" || "yellow" || "blue"){
throw "Invalid Color";
}
}
catch (string e)
{
cout << e << endl;
system("pause");
return -1;
}
if (color1=="red")
{
if (color2 == "red")
{
cout << "red";
}
if (color2 == "blue")
{
cout << "purple" << endl;
}
if (color2 == "yellow")
{
cout << "orange" << endl;
}
}
else if (color1 == "blue")
{
if (color2 == "red")
{
cout << "purple" << endl;
}
if (color2 == "blue")
{
cout << "blue"
}
if (color2 == "yellow")
{
cout << "green" << endl;
}
}
else if (color1 == "yellow")
{
if (color2 == "red")
{
cout << "orange" << endl;
}
if (color2 == "blue")
{
cout << "green" << endl;
}
if (color2 == "yellow")
{
cout << "Yellow" << endl;
}
}
system("pause");
return 0;
}