switch statement - Java, questions about switches and cases? -
switch statement - Java, questions about switches and cases? -
so want action 60 % of time , action 40% of time. , have doing neither. best way can think through switches , making bunch of cases. illustration below if doesn't create sense ya'll. question is, there improve way? there way case 0-5 action 1 in 1 statement?
random rand = new random(50); switch(rand.nextint()) { case 1: { action 1 } break; case 2: { action 1 } break; case 3: { action 1 } break; case 4: { action 1 } break; case 5: { action 1 } break; case 6: { action 1 } break; case 7: { action 2 } break; case 8: { action 2 } break; case 9: { action 2 } break; case 10: { action 2 } break; }
something much more readable imo:
if( math.random() >= probabilityofdoingnothing ){ if( math.random() < 0.6 ){ action1; }else{ action2; } } re. question cases, next equivalent code:
random rand = new random(50); switch(rand.nextint()) { case 1: case 2: case 3: case 4: case 5: case 6: { action 1 } break; case 7: case 8: case 9: case 10: { action 2 } break; } java switch-statement
Comments
Post a Comment