我正在使用一些枚举类型实例化一个对象,并尝试根据这些枚举类型设置一些字符串成员。但是,当我调试并逐步执行时,用于设置字符串的开关每次都会命中,并且每个字符串都设置为每个枚举类型的最后一种情况。
enum Number {
one,
two,
three
};
enum Color {
purple,
red,
green
};
enum Shading {
solid,
striped,
outlined
};
enum Shape {
oval,
squiggle,
diamond
};
Card::Card(Number num, Color colour, Shading shade, Shape shaper) {
number_ = num;
color_ = colour;
shading_ = shade;
shape_ = shaper;
setStrings();
}
void Card::setStrings() {
switch (number_) {
case one:
number_string = "one";
case two:
number_string = "two";
case three:
number_string = "three";
}
switch(color_) {
case purple:
color_string = "purple";
case red:
color_string = "red";
case green:
color_string = "green";
}
switch (shading_) {
case solid:
shading_string = "solid";
case striped:
shading_string = "striped";
case outlined:
shading_string = "outlined";
}
switch (shape_) {
case oval:
shape_string = "oval";
case squiggle:
shape_string = "squiggle";
case diamond:
shape_string = "diamond";
}
}
我使用重载构造函数实例化的每张卡都有number_string ="三个",color_string ="绿色",shading_string ="概述"和shape_string =&# 34;金刚石&#34 ;.
答案 0 :(得分:2)
你需要对switch语句使用break'案例条款,否则它是一个堕落。以下是您的示例和详细信息。 https://10hash.com/c/cf/#idm45440468325552
#include <stdio.h>
int main()
{
int i = 65;
switch(i)
{
case 'A':
printf("Value of i is 'A'.\n");
break;
case 'B':
printf("Value of i is 'B'.\n");
break;
default:
break;
}
return 0;
}
答案 1 :(得分:1)
您的开关箱不正确。您需要在每个案例之后为您的解决方案设置一个break
,否则它会在每个案例中完成,直到它完成,并且当它遇到您想要的案例时不会中断。