我的 switch 语句不允许我使用特定变量作为表达式

My switch statement won´t let me use a particular variable as an expression

我想要的是让用户猜多少次就猜多少次,然后告诉他猜了多少次,这就是为什么我要设置条件对于 for 循环 g!=r,但是,我不知道 C++ 是否允许这样做。

此外,我在尝试编译时遇到的错误是

expression must have a constant value 

expression did not evaluate to a constant" and "case expression not constant

代码如下:

int main()
{
    int r = rand() % 101;
    int g = 0;
    int t = 10;

    std::cout << "Guess a number, human (From 1 to 100)." << std::endl;
    std::cin >> g;

    for (int t = 0; g != r; t++)
    {
        switch (g) {
        case (g == r):
            std::cout << "You won, now get lost!" << std::endl;
            break;
        case (g < r):
            std::cout << "Too low, piece of turd." << std::endl;
            break;
        case (g > r):
            std::cout << "Too high, dubai." << std::endl;
            break;
        default :
            std::cout << "How could you possibly have gotten it wrong, you stupid ape." << std::endl;
        }
    }
    std::cout << "Finally!, it took you " << t << " freaking times!" << std::endl;


    return 0;
}

switch case 检查必须是与原始值进行相等比较的常量表达式。

对于您的情况,您可以使用 if 命令重写 switch。 使用下面的代码,它会编译。

if (g == r)
{
    std::cout << "You won, now get lost!" << std::endl;
}
else if (g < r)
{
    std::cout << "Too low, piece of turd." << std::endl;
}
else if (g > r)
{
    std::cout << "Too high, dubai." << std::endl;
} 
else 
{
    // will not get here, as previous if already cover all cases
    std::cout << "How could you possibly have gotten it wrong, you stupid ape." << std::endl;
}

C++ 开关 http://en.cppreference.com/w/cpp/language/switch

C++ 如果 http://en.cppreference.com/w/cpp/language/if

如果您需要使用开关,您可以尝试使用带有标志的 while 循环并在开关之前进行一些检查:

bool flag = true;
int t = 0;
int x = 0;
while( flag )
{
        t++;
        std::cout << "Guess a number, human (From 1 to 100)." << std::endl;
        std::cin >> g;

        if( g == r )
        {
           x = 1;
           flag = false;
        }
        else if( g < r )
           x = 2;
        else
           x = 3;

        switch (x) {
        case (1):
            std::cout << "You won, now get lost!" << std::endl;
            break;
        case (2):
            std::cout << "Too low, piece of turd." << std::endl;
            break;
        case (3):
            std::cout << "Too high, dubai." << std::endl;
            break;
        default :
            std::cout << "How could you possibly have gotten it wrong, you stupid ape." << std::endl;
        }
}

我将其更改为 if 语句,现在在第一次猜测后,用户无法再次猜测。

好的,新代码在这里:

int main()

{

int r = rand() % 101;

int g = 0;

int t = 0;

std::cout << "Guess a number, human (From 1 to 100)." << std::endl;

std::cin >> g;

for (int t = 0; g = r; t++)
{
    if (g < r)
    {
        std::cout << "Too low." << std::endl;
        std::cin >> g;
    }
    else if (g > r)
        std::cout << "Too high." << std::endl;
        std::cin >> g;
}
std::cout << "Finally!, it took you " << t << " freaking times!" << std::endl;


return 0;

}