如果(aCHAR =='字符'||'另一个字符')问题

Gri*_*fin 0 c++ boolean char

嗨,所以我试图检查一个字符串中的某个字符,以确保它不是\,=,|等,并用"播放器"字符替换空格,如果它不是,但函数每次都返回true,甚至如果char newLoc等于''(空):

screen.get_contents返回一个充满strins的向量容器,
sprite.get_location返回一个带有两个数字的int数组,[0]代表X,[1]是Y.

bool check_collision(Sprite& sprite,int X, int Y, Screen& screen) 
    {
    ////////////////////// check whats already there /////
        char newLoc = screen.get_contents(sprite.get_location()[0]+Y,sprite.get_location()[1]+X);
        if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )
            return true;
        else
            return false;
    };
Run Code Online (Sandbox Code Playgroud)

问题是什么?谢谢!!

Oli*_*rth 8

你需要:

if (newLoc == '|' || newLoc == '/' || ...)
Run Code Online (Sandbox Code Playgroud)

你写的相当于:

if (newLoc == ('|' || '/' || ...))
Run Code Online (Sandbox Code Playgroud)

这相当于:

if (newLoc == 1)
Run Code Online (Sandbox Code Playgroud)

请注意,更简洁的方式可能是:

switch (newLoc)
{
case '|':
case '/':
...
    return true;

default:
    return false;
}
Run Code Online (Sandbox Code Playgroud)