Switch statements can be handy to use instead of using several if-then-else statements to do selected actions depending on a value.
So basically we begin by having an integer :
Now we create the switch statement with the param "val" :
Now we need to add the cases, the cases work in the way that if val = the case number then whatever is in that case happens :
Basically the code above will print "Case 4" because val = 4 so case 4 will be called. You could do rand.nextInt(); for an antiban or something like that to randomize different actions each time. You could also use it for different items if you needed to do different actions by just sending a param to the void. The default case there will run if the case does not exist that val equals. So if I removed case 4 : like in the code below the case default would be run so "Default" would be printed.
So basically we begin by having an integer :
Code:
int val = 4;
Now we create the switch statement with the param "val" :
Code:
int val = 4;
switch(val) {
}
Now we need to add the cases, the cases work in the way that if val = the case number then whatever is in that case happens :
Code:
int val = 4;
switch (val) {
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
case 3:
System.out.println("Case 3");
break;
case 4:
System.out.println("Case 4");
break;
default:
System.out.println("Default");
break;
}
Basically the code above will print "Case 4" because val = 4 so case 4 will be called. You could do rand.nextInt(); for an antiban or something like that to randomize different actions each time. You could also use it for different items if you needed to do different actions by just sending a param to the void. The default case there will run if the case does not exist that val equals. So if I removed case 4 : like in the code below the case default would be run so "Default" would be printed.
Code:
int val = 4;
switch (val) {
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
case 3:
System.out.println("Case 3");
break;
default:
System.out.println("Default");
break;
}


