C++ 在 switch case 语句中,它说“重复的 case 值”作为错误出现。有谁知道为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17225023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:01:03  来源:igfitidea点击:

In a switch case statement, it says "duplicate case value" comes up as an error. Anyone know why?

c++switch-statementdev-c++

提问by Victor Odouard

I am working on a rock paper scissors program, but this time the computer chooses rock half the time, scissors a third of the time, and paper only one sixth of the time. The way I did this was I enumerated six possible computer choice values:

我正在做一个石头剪刀布程序,但是这次计算机有一半的时间选择石头,三分之一的时间选择剪刀,只有六分之一的时间选择纸。我这样做的方法是列举了六个可能的计算机选择值:

enum choicec {rock1, rock2, rock3, scissors1, scissors2, paper};
choicec computer;

But then, after the computer makes its choice, I have to convert these enumerated values to either rock, paper, or scissors. I did this using a switch-case statement:

但是,在计算机做出选择之后,我必须将这些枚举值转换为石头、纸或剪刀。我使用 switch-case 语句做到了这一点:

switch(computer) {
        case rock1 || rock2 || rock3:
            c = 1;
            break;
        case scissors1 || scissors2: //ERROR!
            c = 3;
            break;
        case paper:
            c = 2;
            break;
    }

one is rock, two is paper, and three is scissors. However, on the line where I have error written in as a comment, it gives me this error: [Error] duplicate case value.

一是石头,二是纸,三是剪刀。但是,在我将错误作为注释写入的行上,它给了我这个错误:[Error] 重复大小写值。

I'm not sure why. Any ideas?

我不知道为什么。有任何想法吗?

回答by Mooing Duck

You can't use ||in casebranches. Sorry :(
When you use ||it does a logical or on them, that says "is rock1or rock2or rock3not a zero?". And the answer is yes, at least one of those is not zero. So rock1 || rock2 || rock3is true, which is 1. And scissors1 || scissorsis also true, which is 1. So you have two casebranches for the 1case.

您不能||case分支中使用。对不起:(
当你使用||它的逻辑或在他们身上,上面写着“是rock1rock2rock3不是零?”答案是肯定的,那些至少一个不为零,所以rock1 || rock2 || rock3true,这是1,而且scissors1 || scissorstrue, 即1. 所以你有两个case分支来处理这个1案例。

You should simply use casefallthrough to select multiple conditions:

您应该简单地使用casefallthrough 来选择多个条件:

switch(computer) {
    case rock1: case rock2: case rock3:
        c = 1;
        break;
    case scissors1: case scissors2:
        c = 3;
        break;
    case paper:
        c = 2;
        break;
    default:
        std::cerr << "INVALID COMPUTER MOVE";
}

Also, I always have a default in my case switches. Sometimes mistakes happen, and we definitely want to know if it doesn't hit any of the case branches. I'm also pretty paranoid about missing elsestatements, but about half the time it's ok if there's no else.

另外,我的案例开关总是有一个默认值。有时会发生错误,我们肯定想知道它是否没有命中任何 case 分支。我也很担心缺少else语句,但大约有一半的时间,如果没有else.

回答by aah134

I am not sure what you doing, but switch statement should look like this

我不确定你在做什么,但 switch 语句应该是这样的

switch(computer) 
{
    case rock1:
    case rock2:
    case rock3:
        c = 1;
        break;
    case scissors1:
    case scissors2:
        c = 3;
        break;
    case paper:
        c = 2;
        break;
}

回答by Andy Prowl

That switchstatement does not do what you think.

switch句话并不像你想的那样。

Each casedefines onevalue that the value of computeris matched against. Combining several values with logical disjunction to give the value associated with a single caselabel does notmake the corresponding block be entered when the value of computeris equal to anyof those values, but rather when it is equal to the result of their logical OR combination. Not very meaningful, indeed.

每个都case定义了一个与 的值computer相匹配的值。与逻辑或组合几个值,得到与单个相关联的值case标签并没有使被输入的对应的块时的值computer等于任何这些值,而是当它等于它们的逻辑OR组合的结果。确实不是很有意义。

This is how you could rewrite your switchstatement in order to make more sense:

这是您如何重写您的switch陈述以使其更有意义的方式:

switch(computer) {
    case rock1: // Is it rock1?
    case rock2: // Or perhaps rock2?
    case rock3: // Or maybe rock3?
        c = 1;  // Well, if it's one of the above, do this...
        break;
    case scissors1: // OK, it wasn't. So is it scissors1?
    case scissors2: // Or scissors2?
        c = 3;      // If it's one of the above, do this...
        break;
    case paper: // So is it paper?
        c = 2;
        break;
    default: // Always better to be explicit about this
        break;
}

回答by Taylor Brandstetter

Change it to:

将其更改为:

switch(computer) {
    case rock1:
    case rock2:
    case rock3:
        c = 1;
        break;
    case scissors1:
    case scissors2:
        c = 3;
        break;
    case paper:
        c = 2;
        break;
}

rock1 || rock2 || rock3and scissors1 || scissors2are both expressions which evaluate to "true", hence the conflict.

rock1 || rock2 || rock3scissors1 || scissors2都是评估为“真”的表达式,因此存在冲突。

回答by Arun Kumar

The expression used in the switch statement must be integral type ( int, char and enum). In the Switch statement, all the matching case execute until a break statement is reached and Two case labels cannot have the same value.

switch 语句中使用的表达式必须是整型(int、char 和 enum)。在 Switch 语句中,所有匹配的 case 都会执行,直到到达 break 语句并且两个 case 标签不能具有相同的值。

But in the above case with logical or condition. At first case: rock1 || rock2 || rock3:This will evaluate to 1 and second case scissors1 || scissors2:will also evaluate to 1. This is cause error as said Two case labels cannot have the same value.

但在上述情况下,逻辑或条件。首先 case: rock1 || rock2 || rock3:This 将评估为 1,第二个case scissors1 || scissors2:也将评估为 1。这是导致错误的原因,因为所述两个案例标签不能具有相同的值。

This is the reason compiler complains and giving an error:

这就是编译器抱怨并给出错误的原因:

Compiler Error: duplicate case value

Compiler Error: duplicate case value

To solve this convert to

为了解决这个转换为

switch(computer) {
        case rock1: 
        case rock2:  
        case rock3:
            c = 1;
            break;
        case scissors1:
        case scissors2: //Now will not give any error here...
            c = 3;
            break;
        case paper:
            c = 2;
            break;
    }