1

I have this simple code. An integer which value is 0 and a null String. Simple enough. Yet when I run the program it prints "a" instead of "z". I don't know where's the problem and what am I missing.

public static void main(String[] args) {

    int classCode = 0;
    String classString = null;
    switch(classCode) {
    case 0:
        classString = "z";
    case 10:
        classString = "a";
        break;
    case 11:
        classString = "b";
        break;
    case 20:
        classString = "c";
        break;
    case 21:
        classString = "d";
        break;
    case 30:
        classString = "e ";
        break;
    case 31:
        classString = "f";
        break;
    }
    System.out.println(classString);

}
Virginia
  • 41
  • 6

1 Answers1

2

You have forgotten to put a break after the first case.

switch(classCode) {
case 0:
    classString = "z";
    // missing a break here
case 10:
    classString = "a";
    break;
leeyuiwah
  • 6,562
  • 8
  • 41
  • 71