Unity C# 中使用多变量 switch 语句时出现错误 CS0019

问题描述 投票:0回答:1

我不断收到“错误 CS0019:操作员”<' cannot be applied to operands of type 'bool' and 'double'". Both of the variables being used are doubles and I have tried a few different ways to make this work. My code is here: `

    switch((xPos, yPos))
    {
        case ((-7.445 < xPos < -6.795) && (4.15 > yPos > 3.45)):
            xPos = -7.12;
            yPos = 3.805;
            break;
        case ((-7.445 < xPos < -6.795) && (3.35 > yPos > 2.65)):
            xPos = -7.12;
            yPos = 3.005;
            break;
        case ((-7.445 < xPos < -6.795) && (2.55 > yPos > 1.85)):
            xPos = -7.12;
            yPos = 2.205;
            break;
        case ((-7.445 < xPos < -6.795) && (1.75 > yPos > 1.05)):
            xPos = -7.12;
            yPos = 1.405;
            break;
        case ((-7.445 < xPos < -6.795) && (0.95 > yPos > 0.25)):
            xPos = -7.12;
            yPos = 0.605;
            break;
        case ((-7.445 < xPos < -6.795) && (0.15 > yPos > -0.55)):
            xPos = -7.12;
            yPos = -0.195;
            break;
        case ((-7.445 < xPos < -6.795) && (-0.65 > yPos > -1.35)):
            xPos = -7.12;
            yPos = -0.995;
            break;
        case ((-7.445 < xPos < -6.795) && (-1.45 > yPos > -2.15)):
            xPos = -7.12;
            yPos = -1.795;
            break;
        case ((-7.445 < xPos < -6.795) && (-2.25 > yPos > -2.95)):
            xPos = -7.12;
            yPos = -2.595;
            break;
        case ((-7.445 < xPos < -6.795) && (-3.05 > yPos > -3.75)):
            xPos = -7.12;
            yPos = -3.395;
            break;
        case ((-7.445 < xPos < -6.795) && (-3.85 > yPos > -4.55)):
            xPos = -7.12;
            yPos = -4.195;
            break;`

我尝试使用以下示例:

case ((-7.445 < xPos < -6.795), (-3.85 > yPos > -4.55)): case 1 when ((-7.445 < xPos < -6.795), (-3.85 > yPos > -4.55)): case 1 when (-7.445 < xPos < -6.795), (-3.85 > yPos > -4.55): case 1 when -7.445 < xPos < -6.795, -3.85 > yPos > -4.55:
到目前为止还没有任何效果。我使用它来确定对象在网格上的放置位置,因此我想同时检查 x 位置和 y 位置,而不是出于显而易见的原因使用嵌套 switch 语句。

c# unity-game-engine 2d
1个回答
0
投票

对于所有情况,请将您的条件更新为以下内容: 当你写-7 < xPos < -6.795, as soon as -7

case ((-7.445 < xPos && xPos < -6.795) && (4.15 > yPos && yPos > 3.45)):
© www.soinside.com 2019 - 2024. All rights reserved.