-1

For the following switch statement:

if a value between 0-9 is selected, output is fine. If value greater than 9 is selected, output is always a lowercase 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]);
Christian Baker
  • 375
  • 2
  • 7
  • 22
  • 6
    Add break statements in each case. – Alexis C. Nov 17 '13 at 21:15
  • 3
    You could achieve the same result easier by doing a little addition with character values, by the way! :) – kviiri Nov 17 '13 at 21:17
  • What a silly mistake! -1: Your code **WON"T COMPILE** in Java. –  Nov 17 '13 at 21:18
  • @Desolator, care to elaborate? Even though it is obviously semantically incorrect I fail to see any issue with the syntax. – kviiri Nov 17 '13 at 21:40
  • @kviiri Sorry for that. The code won't compile because there is no `break` at all. This is a compilation requirement in Java, unlike C++. This is why a `break` is required even after `default:`. –  Nov 17 '13 at 21:41
  • @Desolator, that's not true. Java permits fall-through in switch-cases. It's even explicitly mentioned in the specification (example 14.11-1 in Java Language Specification 7). I suggest you reverse your downvote. – kviiri Nov 17 '13 at 21:47
  • @kviiri I don't mean fall-through. I mean *there must a `break;` after the final `case`, otherwise the code will never compile. –  Nov 17 '13 at 21:49
  • 1
    @Desolator You're wrong. My compiler compiles the code just fine without any breaks, and there's no reason why it shouldn't because the specification doesn't mention breaks being necessary for switch-cases to work. – kviiri Nov 17 '13 at 21:52

3 Answers3

5

A case statement is a form of standidised goto statement, it goes to the case statement and then continues on as usual. To get the behaviour you desire you need a

break;

at the end of each case

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0

Add break with all case blocks like

case 10: 
  character[i] = "A";
  break;
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
0

Where is the break of the cases man!

switch(random[i]){
 case 10: ....
  break;
 case 11: ....
  break;
 //and so on
}
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32