Java switch double duplicate case

前端 未结 2 1120
无人共我
无人共我 2021-01-18 07:11

I\'m creating a chess game with java.

As you know when you start out the chess game you have two of each \"Captains\" (sorry I\'m not sure what the term is) I have c

相关标签:
2条回答
  • 2021-01-18 07:41

    I assume you tried OR by putting ||, but in switch case statements you cant use this operator. Therefore you just use if

    if(j==1 || j==8){
     Rook tower = new Rook("");
                return tower.getBrik();
    }else if(j==2 ||j==7 || j==6 || j==7){
    
    Bishop bishop = new Bishop("");
                return bishop.getBrik();
    }
    .
    .
    .
    
    0 讨论(0)
  • 2021-01-18 07:47

    Because of fall through (execution continues to the next case statement unless you put a break; at the end, or of course, as in your case, a return), you can just put the cases under each other:

    ...
    case 1:
    case 8:
        Rook tower = new Rook("");
        return tower.getBrik();
    case 3:
    case 6:
        Bishop bishop = new Bishop("");
        return bishop.getBrik();
    ...
    
    0 讨论(0)
提交回复
热议问题