loops - Java - Repeat Function for Specific Numbers -
i exceptionally new programming, working on improving skills programmer. recently, gave myself challenge determine multiples of given number made of distinct digits. have gotten of work, still need create code apply every number multiple of input one. code have working far follows:
integer numbera = 432143; integer numberb = numbera; integer[] digitarray = new integer[numbera.tostring().length()]; int index; (index = 0; index < digitarray.length; index++) { digitarray[index] = (numbera % 10); numbera /= 10; } int repeats = 0; (int = 0; < digitarray.length; i++) { (int j = 0; j < digitarray.length; j++) { if ((i != j) && (digitarray[i]==digitarray[j])) repeats = repeats + 1; } } if (repeats == 0) { system.out.println(numberb); }
this determine if number made of distinct digits, and, if is, print out. have spent quite bit of time trying create rest of code work, , i've come with:
integer number = 1953824; integer numbera = number; integer numberb = numbera; (integer numberc = number; numberc.tostring().length() < 11; numberc = numberc + number) { integer[] digitarray = new integer[numbera.tostring().length()]; int index; (index = 0; index < digitarray.length; index++) { digitarray[index] = (numbera % 10); numbera /= 10; } int repeats = 0; (int = 0; < digitarray.length; i++) { (int j = 0; j < digitarray.length; j++) { if ((i != j) && (digitarray[i]==digitarray[j])) repeats = repeats + 1; } } if (repeats == 0) { system.out.println(numberb); } }
i can't figure out why, prints whatever number bunch of times if made of distinct digits, , leaves blank if not. if tell me why occurring, or tell me need prepare it, superb. remember, new programming, please give short explanation terms utilize @ out of ordinary. eager learn, know little. give thanks time, , appreciate , help can give me.
you assign value of numbera numberb (which value of number) right before loop. after that, numberb never modified or assigned new value, every pass through loop, you're printing value of numberb, 1953824 in case.
there several corrections can made accomplish result desire, while cleaning code little. first thing alter print statement print right number:
system.out.println(numberc);
since numberc variable beingness updated loop, that's you'll want conditionally print out if there no repeat digits. since we've replaced numberb numberc, means numberb not longer needed, can delete declaration it.
now, next issue when you're defining digital array - should utilize length of numberc, not numbera. also, within loop, should assign numbera value of numberc, or else nil 0s stored in digitarray. overall, here's should like.
integer number = 1953824; integer numbera = number; (integer numberc = number; numberc.tostring().length() < 11; numberc = numberc + number) { integer[] digitarray = new integer[numberc.tostring().length()]; numbera = numberc; int index; (index = 0; index < digitarray.length; index++) { digitarray[index] = (numbera % 10); numbera /= 10; } int repeats = 0; (int = 0; < digitarray.length; i++) { (int j = 0; j < digitarray.length; j++) { if ((i != j) && (digitarray[i] == digitarray[j])) repeats = repeats + 1; } } if (repeats == 0) { system.out.println(numberc); } }
this should produce desired result. seems work on machine :)
if want, can take jeffrey's suggestion , alter integer primitive int avoid overhead of boxing. however, still need utilize integer class utilize tostring() method, can accomplish using integer.valueof():
integer.valueof(numberc).tostring()
java loops for-loop
No comments:
Post a Comment