Replacing a number with a word Java -
been digging around internet day looking solution problem. i've started programming 2 weeks ago , fact don't know proper java lingo yet reason i'm not finding i'm looking for.
anyway program supposed to:
print out numbers 1 100 , if during iteration program comes across number that's divisible 3 has replace number "crackle", if comes across number that's divisible 5 has replace number "pop" , comes across number that's divisible both has replace number "cracklepop
i can't print cracklepop numbers divisable both 3 , 5, can print crackle & pop but not replace numbers.
i've tried using:
integer.tostring(); , integer.valueof(); , integer.parseint();
none of can work. code far:
int counter = 0; int max = 100; (int = 1; <= max; i++){ if (counter % 3 == 0) { system.out.println("crackle"); } else if(counter % 5 == 0){ system.out.println("pop"); } else if (counter % 3 == 0 && counter % 5 == 0){ system.out.println("cracklepop"); } system.out.println(counter); counter++; }
if if suggest solution robust way of writing program too.
there no need counter
variable. can use i
counter itself.
also should place check both being divisible 3 , 5 first:
int max = 100; (int = 1; <= max; i++){ if (i % 3 == 0 && % 5 == 0){ system.out.println("cracklepop"); } else if (i % 3 == 0) { system.out.println("crackle"); } else if(i % 5 == 0){ system.out.println("pop"); } else { system.out.println(i); } }
the last else
make sure replace number corresponding string if matches 1 of conditions of being divisible 3 and/or 5.
Comments
Post a Comment