代码之家  ›  专栏  ›  技术社区  ›  ken4ward

用Java打印每行10行数字

  •  -2
  • ken4ward  · 技术社区  · 7 年前

    如何每行打印10个数字?我想要这样的输出:

    1 2 3 3 4 5 6 7 8 9 10
    1 4 9 16 25 36 49 64 81 100
    1 8 27 64 125 216 343 512 729 1000
    ................
    ...................
    

    但我现在所得到的是混乱。

    1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 4 16 64 256 1024 5 25 125 625 3125 6 36 216 1296 7776 7 49 343 2401 16807 8 64 512 4096 32768 9 81 729 6561 59049 10 100 1000 10000 100000
    

    这是一个片段

    public class NumberOnly
    {
        public static void main (String [] args)
        {
            for(int count = 1; count <= 10; count++) {
    
                     System.out.print(count + " ");
                     System.out.print((int) Math.round(Math.pow(count, 2))  + " ");
                     System.out.print((int) Math.round(Math.pow(count, 3))  + " ");
                     System.out.print((int) Math.round(Math.pow(count, 4))  + " ");
                     System.out.print((int) Math.round(Math.pow(count, 5))  + " ");
            }
        }
    }
    

    如何解决此问题?

    1 回复  |  直到 7 年前
        1
  •  2
  •   halfer    7 年前

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