IDEA(C ++):
这个想法很简单,如果您未满21岁,并且接受全日制教育,就符合资格(不知道是什么,这只是作业)。如果您不符合条件,则必须告诉用户原因。
int main()
{
string education;
int age;
cout << "Are you in full time education? (y/n): ";
cin >> education;
cout << "\nEnter your age: ";
cin >> age;
system("cls");
if (((education == "yes" || education == "y")) && (age <= 21))
{
cout << "You are eligible.";
}
else if (((education == "yes" || "y")) && (age > 21))
{
cout << "You are not eligible because you are over 21.";
}
else if (((education == "no" || "n")) && (age <= 21))
{
cout << "You are not eligible because you are not in full time education.";
}
else if (((education == "no" || "n")) && (age > 21))
{
cout << "You are not eligible because you are not in full time education and you are over 21.";
}
else
{
cout << "There is a problem with your input.";
}
}
问题:
现在,如果我输入我未接受全日制教育且不超过21岁,则输出为“您不符合条件,因为您超过21岁。”从技术上讲是正确的,但它应该给我“您不是合格,因为您尚未接受全日制教育,并且您已超过21岁。代替!
注意事项:
您不能像这样使用or运算符
a == 'first' || 'second' // education == 'yes' || 'y'
为了说“如果a
等于first
或second
”,则必须在右侧也重复a==
:
a == 'first' || a == 'second' // education == 'yes' || education == 'y'