Java syntax error while trying to learn for loops

I try to teach myself Java, and I learn about for loops. I am trying to write a short and simple program that gives me the following result:

1

fourteen

1 4 9

1 4 9 25

I have a feeling that I worked in the exponential part. My source code is as follows:

public class Forloop {
public static void main(String[] args) {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(int j = Math.pow(j,i));
        }
    System.out.println();
    }
}

}

Can someone help me in where I did wrong and maybe fix it. Thank you very much.

+4
source share
4 answers

You cannot have a variable declaration in your print statement. Just write like this:

public static void main(final String[] args) {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(Math.pow(j, i));
        }
        System.out.println();
    }
}

. - , j, , :

public static void main(final String[] args) {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= i; j++) {
            int exp = (int) Math.pow(j, i);
            System.out.print(exp);
        }
        System.out.println();
    }
}

, @JigarJoshi, Math.pow() , . , :

public static void main(final String[] args) {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j * j + " ");
        }
        System.out.println();
    }
}
+6

public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print((int) Math.pow(j, 2));
            System.out.print(" ");
        }
        System.out.println();
    }
}

1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
+2

, . .

public static void main(final String[] args) {
    int j2;
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j2 = (int) Math.pow(j, i));
        }
        System.out.println();
    }
}
0

int j = Math.pow(j,i) System.out.print. ? System.out.print(Math.pow(j,i));

0

All Articles