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'.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top