Can anyone tell me why my console does not show any results?

1

Get a n value and print the following table using the for :

1
2   4
3   9  27
4  16  64  256
n  n²  n³  ...  n^n

I tried:

import java.util.Scanner;

public class Questao4 {

    public void Cadeia() {
        Scanner num = new Scanner(System.in);
        System.out.printf("Informe um número: ");
        int n = num.nextInt();

        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {

                System.out.println(Math.pow(i,j) + "");
            }
            System.out.println("");
        }

    }

}
    
asked by anonymous 23.08.2018 / 03:54

1 answer

3

I made some corrections and adjustments, and I have listed for you to understand the effects of each change:

  • Changed condition i < n by i <= n so that the number of lines match the typed;

  • Changed condition j < n by j <= i , to meet the statement;

  • created a variable r to store the result of pow , which is a double ;

  • Added variable r in print based on version 1 of the question, with calculation

  • Removing println from print , is unnecessary if you only want to break the line.

class Questao4 {
    public void Cadeia() {
        Scanner num = new Scanner(System.in);
        System.out.printf("Informe um número: ");
        System.out.println();

        int n = num.nextInt();
        double r;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                r = Math.pow(i,j);
                System.out.print(r + "        ");
            }
            System.out.println();
        }
    }
}

See working at IDEONE

    
23.08.2018 / 04:22