so I have this code:
int in = 496;
boolean b = true;
Sting s = "":
if (b) {
int test = 0;
System.out.print(in + ":");
for (int i = 1; i < in; i++) {
if (in % i == 0) {
test += i;
s += i + " ";
}
}
System.out.println(" ");
} else {
System.out.println(in + " no factorials");
}
and it prints like this:
496:1 2 4 8 16 31 62 124 248
but I want to revers the factorials output to look like this:
496:248 124 62 31 16 8 4 2 1
I tried to reverse the for loop, but it work, so any ideas guys?
Changing the loop is indeed what you'd need:
for (int i = in-1; i > 0; i--) {
By the way, how did you post your code? What you pasted (or typed) does not print any factorials, and s =+ ...
should probably be s += ...
, right? Please be as concise as possible, so we have to guess/correct only the real problem, not any typos.
Untested, but why won't this work?
int in = 496;
boolean b = true;
Sting s = "":
if (b) {
int test = 0;
System.out.print(in + ":");
for (int i = in-1; i > 0; i--) {
if (in % i == 0) {
test += i;
s =+ i + " ";
}
}
System.out.println(" ");
} else {
System.out.println(in + " no factorials");
}
also, it would help everyone if you used better variable names... once your methods get a bit more complicated it'll be hard to follow 's', 'b', 'i', and even 'in'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.