How to write on other values placed after the operators case?

后端 未结 1 665
醉梦人生
醉梦人生 2021-01-21 13:12

Good afternoon, I am using the switch construction, but I don’t know how to write correctly for other values after the case operators.

In the DisplayData code, in the swi

相关标签:
1条回答
  • 2021-01-21 13:53

    The problem is default:.

    The switch run like this: it finds the case which corresponds to the key and starts executing code from this point on until it hits a break;. The default: case is a catch-all: it'll like a condition which would always be true.

    To illustrate, I like this example: here when the user enters a number, it prints to the console the names of all the month from that point up to December.

    int month = 5;
    
    switch (month) {
        case 1:  println("January");
        case 2:  println("February");
        case 3:  println("March");
        case 4:  println("April");
        case 5:  println("May");
        case 6:  println("June");
        case 7:  println("July");
        case 8:  println("August");
        case 9:  println("September");
        case 10: println("October");
        case 11: println("November");
        case 12: println("December");
                 break;
        default: println("Error: no valid month selected.");
                 break;
    }
    

    Notice how default is at the end. That's because since it's always "true" and since the switch will select only one case and then run until a break statement, if I put it anywhere else it will always be selected when it's read. See by modifying the example like this:

    int month = 5;
    
    switch (month) {
        default: println("Whatever, I'm not printing month anymore!");
                 break; // no case after this point will ever be read
        case 1:  println("January");
        case 2:  println("February");
        case 3:  println("March");
        case 4:  println("April");
        case 5:  println("May");
        case 6:  println("June");
        case 7:  println("July");
        case 8:  println("August");
        case 9:  println("September");
        case 10: println("October");
        case 11: println("November");
        case 12: println("December");
                 break;
    }
    

    This is exactly what happens in your program. In DisplayData(), right at tthe beginning there's a default case which overrides all the other cases:

    void Displaydata() {
    
      switch(readIncome[0]) {
      case 10:
      default: // <--- HERE THIS IS ALWAYS SELECTED
        minutess.setCaptionLabel(readIncome[1]+" Мин.");
        min=readIncome[1];
        break; // <--- EVERYTHING AFTER THIS IS IGNORED
    

    You can re-write with the correct syntax like this:

    void Displaydata() {
      switch(readIncome[0]) {
      case 10:
        minutess.setCaptionLabel(readIncome[1]+" Мин.");
        min=readIncome[1];
        break;
      case 20:
        button.label(readIncome[1]+" Мин."); //ImageButton
        min=readIncome[1];
        break;
      case 30:
        if(readIncome[1]==1)P4.setText("on");
        if(readIncome[1]==0)P4.setText("off");// CheckBox
        break;
      case 40:
        inputPULI.setText("Bullet - "+readIncome[1] ); //Numberbox int
        break;
      case 70:
        inputNapryzenieKV.setText("Voltage - "+readIncome[1] ); //Numberbox float
        break;
      case 60:
        Vin.setText("Voltage K.V - "+readIncome[1] );
        break;
      case 50:
        if(readIncome[1]==1)CheckBoxuvum.setText("+"); //RadioButton
        if(readIncome[1]==0)CheckBoxuvum.setText("-");
        break;
      default:
        println("DisplayData(): no case selected.");
        break; // technically not necessary, but I like my switches tidy
      }
    }
    

    Have fun!

    0 讨论(0)
提交回复
热议问题