开发者

Multiple/repeating cases in a Java switch statement

开发者 https://www.devze.com 2023-03-27 06:22 出处:网络
I would like to know how Java handles multiple identical instances of the same case. I think the following makes sense, conceptually:

I would like to know how Java handles multiple identical instances of the same case. I think the following makes sense, conceptually:

switch (someIntegerValue)
{
   case 1:
   case 2:
      DoSomethingForBothCases();
      break;
   case 3:
      DoSomethingUnrelated();
      break;
   case 1:
      DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
   case 2:
      DoSomethingForCase2ThatReli开发者_开发技巧esUponExecutionOfTheEarlierFunctionCall();
      break;
}

Essentially, I would like to have a chunk of code executed for either case 1 or 2 (using fall-through), but then later on, have a chunk of code only executed for case 2.

Rather, is the following necessary, instead?

switch (someIntegerValue)
{
   case 1:
      DoSomethingForBothCases();
      DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
   case 2:
      DoSomethingForBothCases();
      DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
   case 3:
      DoSomethingUnrelated();
      break;
}

My actual code is more complex, but would use the same principle (i.e. something like "case 1: nope; alright... case 2: yep! execute this code!; case 3: nope; case 1 again?: still nope!; case 2 again?: yep! execute this code; no more cases: All Done!")


Anything wrong with two switch statements?

switch (someIntegerValue) {
   case 1:
   case 2:
      DoSomethingForBothCases();
      break;
   case 3:
      DoSomethingUnrelated();
      break;
}

switch (someIntegerValue) {
   case 1:
      DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
   case 2:
      DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
}

That's what I would do.


You cannot repeat cases in a Java switch statement, it is a compile error. You will need to do as you have suggested, which actually looks like a good factoring.


You won't be able to do it. You can't have duplicate cases, the compiler will throw a hissy fit lol. I understand your logic in that you would like to check each case and then continue on and check other cases, but the break statement would pull you out of the switch-case statement anyways. What I would recommend is that you consider using a loop (i.e. for, while) if you want to check different things continuously. Coupling if statements with a loop will help you to execute bits of code before others.

Or if you really like the switch statements, you can create multiple switch-cases so that certain things happen before others.


How About this? NOTE: I don't know that much Java, only Swift 2

var someIntegerValue = 1

func someSwitch(){

 switch (someIntegerValue) {
    case 1:
    break;
    case 2:
       DoSomethingForBothCases();
    break;
    case 3:
  DoSomethingUnrelated();
  break;
  }
}

where you have a two button actions,

some action button NEXT

someIntegerValue +=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch

some action button 2 Back

someIntegerValue -=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号