Switch语句总是选择相同的情况

时间:2013-11-17 21:15:04

标签: java

对于以下switch语句:

如果选择0-9之间的值,则输出正常。如果选择大于9的值,则输出始终为小写z。

for (int i = 0; i < 3; i++)
    {
        random[i] = randomnumber.nextInt(36);
        if (random[i] > 9)
        {
            switch(random [i])
            {
            case 10: character[i] = "A";
            case 11: character[i] = "B";
            case 12: character[i] = "C";
            case 13: character[i] = "D";
            case 14: character[i] = "E";
            case 15: character[i] = "F";
            case 16: character[i] = "G";
            case 17: character[i] = "H";
            case 18: character[i] = "I";
            case 19: character[i] = "J";
            case 20: character[i] = "K";
            case 21: character[i] = "L";
            case 22: character[i] = "M";
            case 23: character[i] = "N";
            case 24: character[i] = "O";
            case 25: character[i] = "P";
            case 26: character[i] = "Q";
            case 27: character[i] = "R";
            case 28: character[i] = "S";
            case 29: character[i] = "T";
            case 30: character[i] = "U";
            case 31: character[i] = "V";
            case 32: character[i] = "W";
            case 33: character[i] = "X";
            case 34: character[i] = "Y";
            case 35: character[i] = "Z";
            }
        }
        else
            character[i] = Integer.toString(random[i]);

3 个答案:

答案 0 :(得分:6)

case语句是standidised goto语句的一种形式,它转到case语句,然后像往常一样继续。为了获得你想要的行为,你需要一个

break;

在每个案件的最后

答案 1 :(得分:1)

添加break所有案例块,例如

case 10: 
  character[i] = "A";
  break;

答案 2 :(得分:1)

案件的break人在哪里!

switch(random[i]){
 case 10: ....
  break;
 case 11: ....
  break;
 //and so on
}