2016-06-07 69 views
1

我想设置不同的时间相等,以便我可以继续与其他语句。但由于某种原因,它似乎不想这样做。不仅如此,它似乎只承认我拥有的第一个“TIE”,即使它也应该承认其他的。我究竟做错了什么?为什么我不能在If语句中定义多个相等的变量?

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

请取消您的Wall Of Code并将其替换为仅包含重现问题所需代码的简化示例。 –

+4

它看起来像你正在编写的代码基于你有*猜测*语法的手段来表示。这只会让你陷入麻烦。这里值得回到基础知识来了解每个操作符*实际*所做的事情。 –

回答

3

此:(time1 < time2 && time3)意味着time1大于time2time3为真(非零)。

什么你大概的意思是:(time1 < time2 && time1 < time3)

这:(time1 == (time2 == time3))意味着time1等于真(1)如果time2等于time3,否则time1等于假(0)。

你可能意思是(time1 == time2 && time1 == time3)

所有其他比较都有类似的问题。每个二进制比较结果都是一个简单的布尔值。你不能像你在演讲中那样加入比较。

+0

谢谢md5i,说实话,你是救命恩人。 我很欣赏帮助以及评论,我现在正在大学读C++,所以仍然有很多我不知道。 –