打印一系列数字

时间:2019-10-03 16:00:58

标签: java loops count

我正在尝试在“值”列中打印1-20中的所有值。

i的平方值和立方值增加时,所有平方值。

运行代码时,出现错误。有人可以帮我吗!

package jsquaresandcubes;
public class JSquaresandCubes {


        public static void main(String[] args) {
            //display a title at top 
            System.out.printf ("Value        Square          Cube\n");

            //display numbers 1-20 
            for(int i=0; i<20; i++){
              int square = i * i;
              int cube = i * i * i;

              System.out.printf("%4d%c  %8.6f   %8.6f   %8.6f\n", 
                   i,  square, cube);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

    public static void main(String[] args) {
    //display a title at top 
    System.out.printf ("Value        Square          Cube\n");

    //display numbers 1-20 
    for(int i=0; i<20; i++){
      int square = i * i;
      int cube = i * i * i;

      System.out.printf("%-14d %-14d %-14d \n", 
           i,  square, cube);
    }
}