Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




我试图将不同的时间设置为彼此相等,以便可以继续执行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;
}

最佳答案

这是:(time1 < time2 && time3)表示time1大于time2,而time3为true(非零)。

您可能的意思是:(time1 < time2 && time1 < time3)
这:(time1 == (time2 == time3))表示如果time1等于time2,则time3等于true(1),否则time1等于false(0)。

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

您的所有其他比较都有类似的问题。每个二进制比较都会产生一个简单的 bool(boolean) 值。您无法以言语方式进行比较。

关于c++ - 为什么不能在If语句中定义多个变量以彼此相等? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37669931/

10-13 03:20