为什么我不能在If语句中定义多个变量以使其彼此相等?

时间:2016-06-07 03:17:53

标签: c++

我试图将不同的时间设置为彼此相等,以便我可以继续使用else if语句。但出于某种原因,它似乎不想这样做。不仅如此,它似乎只承认我拥有的第一个“TIE”,即使它应该也能识别其他的“TIE”。我做错了什么?

if (time1 < time2 && time3)
{
    cout << "\nCongratualations " << racer1 << "!!! " << "You are the winner!!" << endl;
    cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}
else if (time2 < time1 && time3)
{
    cout << "\nCongratualations " << racer2 << "!!! " << "You are the winner!!" << endl;
    cout << "\n***** Your winning time is: " << time2 << " *****" << endl;
}
else if (time3 < time1 && time2)
{
    cout << "\nCongratualations " << racer3 << "!!! " << "You are the winner!!" << endl;
    cout << "\n***** Your winning time is: " << time3 << " *****" << endl;
}
else if ((time1 == time2) < time3)
{
    cout << "\nWe have a TIE " << racer1 << " and " << racer2 << " win!!" << endl;
    cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}
else if ((time2 == time3) < time1)
{
    cout << "\nWe have a TIE " << racer2 << " and " << racer3 << " win!!" << endl;
    cout << "\n***** Your winning time is: " << time2 << " *****" << endl;
}
else if ((time3 == time1) < time2)
{
    cout << "\nWe have a TIE " << racer1 << " and " << racer3 << " win!!" << endl;
    cout << "\n***** Your winning time is: " << time3 << " *****" << endl;
}
if (time1 == (time2 == time3))
{
    cout << "\nWe have a 3 way TIE!! No winner for this Race..." << endl;
    cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}

1 个答案:

答案 0 :(得分:3)

这:(time1 < time2 && time3)表示time1大于time2time3为真(非零)。

你的意思是:(time1 < time2 && time1 < time3)

这:(time1 == (time2 == time3))表示如果time1等于time2,则time3等于true(1),否则time1等于false(0) )。

你的意思是(time1 == time2 && time1 == time3)

您的所有其他比较都有类似的问题。每个二进制比较产生一个简单的bool。你不能像在演讲中那样加入比较。