在 switch case 语句中,它表示“重复的 case 值".作为一个错误出现.有谁知道为什么?

2022-01-19 00:00:00 switch-statement c++ dev-c++

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

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;

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

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(computer) {
        case rock1 || rock2 || rock3:
            c = 1;
            break;
        case scissors1 || scissors2: //ERROR!
            c = 3;
            break;
        case paper:
            c = 2;
            break;
    }

一是石头,二是纸,三是剪刀.但是,在我将错误写为注释的那一行,它给了我这个错误:[Error] duplicate case value.

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.

我不知道为什么.有什么想法吗?

I'm not sure why. Any ideas?

推荐答案

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

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

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

相关文章