if statement - How to make "if" condition change automatically depending on Command Line Argument in Java? -
in code, if condition should change depending on command line argument. example in code below:
int = integer.parseint(args[0]); (int = 0; < 100000; i++) { if (a == 2) if ((i % 25 == 0) || ((i+1) % 25 == 0) // else if (a == 3) if ((i % 25 == 0) || ((i+1) % 25 == 0) || ((i + 2) % 25 == 0)) // //... }
if command line argument increases, number of conditions inside "if" should increase well. "if" condition depends on command line argument can between 1 , 50. need optimal way avoid writing "if" condition 50 times.
if don't mind using boolean flags, can this:
boolean matched; (int iter = 0; iter < a; iter++) { if ((i + iter) % 25 == 0) { matched = true; break; } } if (matched) { //... }
i haven't tested this, should work. loop through and, every number iter
between 0 , a-1, inclusive, check if i+iter mod 25 zero. if is, sets flag true
, regardless if already. emulates action of ||
, goes through each and, if true, stop comparing , return true
. if wanted &&
, set matched
true
default, false
if fails , break.
you wrap in method if you'd like:
boolean functionnamehere(int a, int i) { boolean matched; (int iter = 0; iter < a; iter++) { if ((i + iter) % 25 == 0) { matched = true; break; } } return matched; }
depending on how i
, a
set up.