本文介绍了在 switch case 语句中,它表示“重复 case 值";出现错误.有谁知道为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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] 重复大小写值.

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;
}

这篇关于在 switch case 语句中,它表示“重复 case 值";出现错误.有谁知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:41