在 switch case 语句中,它表示“重复的 case 值".作为一个错误出现.有谁知道为什么? [英] In a switch case statement, it says "duplicate case value" comes up as an error. Anyone know why?

查看:24
本文介绍了在 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] 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;
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆