简体   繁体   中英

Java Variable expected inside a for loop

I'm pretty new here and new to Java in general. I'm trying to solve a projecteuler question and I thought I had a solution but got stick with this error that I cannot fix. Just to give you an idea of what I was going for(in case it wasn't clear) this is the question: "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" I don't want a solution but rather any help with getting rid of the "Java Variable expected inside a for loop." error would be appreciated.

public class Main {
    public static void main(String[] args) {
        int remainder = 0;
        int remainders[] = new int[20];
        int j = 1;
        int remaindersMax = 0;
        while (true) {
            for (int i = 1; i <= 20; i++) {
                j % i = remainders[i];
                for (int k = 0; k < remainders.length; k++) {
                    if (remaindersMax < remainders[i]) {
                        remaindersMax = remainders[i];
                    }
                }
            }
            if (remaindersMax == 0) {
                break;
            }
            System.out.println(j);
        }
    }
}
j % i = remainders[i];

is invalid syntax. The left hand side (LHS) of a variable assignment can only contain a variable, not an expression. The right hand side (RHS) can contain arbitrarily complex expressions. You want to assign the array remainders at index i the value of the modulo operation. Swap LHS and RHS to make your program compile:

remainders[i] = j % i;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM