vendredi 24 février 2017

Better way to handle n number of if-else if in java

I was wondering if there is any better way to handle n no. of if/else-if block in Java.

I have a situation where I need to print different values based on n no. of if/else-if/conditions blocks like

if(p==1 && q==r)
  System.out.println("Condition 1");
else if(r==p && q==9)
  System.out.println("Condition 2");
else if(z==1 && s==r)
  System.out.println("Condition 3");
else if(p==1 || x==r && y==7)
  System.out.println("Condition 4");
else if(q==z && y==r || p==4)
  System.out.println("Condition 5");
else if(x==z && r==5 || z==30)
  System.out.println("Condition 6");
else if(s==1 || q==x)
  System.out.println("Condition 7");
else if(r==14 || q==r++ || z==y)
  System.out.println("Condition 8");
else if(q==18 && s==r || p==90)
  System.out.println("Condition 9");
else if(y==19 || q==89)
  System.out.println("Condition 10");
.
.
.
.

Is there any other way to handle it instead of putting multiple if/else if statements so that if later on any new condition come, it will be easy to handle it.

Aucun commentaire:

Enregistrer un commentaire