How to align String on console output

前端 未结 3 1385
你的背包
你的背包 2020-12-16 14:38

I\'ve to write code for:

\"Multiplication

3条回答
  •  情歌与酒
    2020-12-16 15:42

    Formatting of output can be done using the System.out.format("","") method this method contain two input parameter first define the formatting style and second define value to be print. Let us assume you want to the n digit value right align. you will pass the first parameter "%4d".

    For the left align use -ve %-nd

    For right align use +ve %nd

     for(int i=1; i<13; i++){
           for(int j=1; j<13; j++){
                System.out.format("%4d", i * j);  //each number formatted to a width of 4 so use %4d in the format method.
        }
        System.out.println();  // To move to the next line.
    }
    

提交回复
热议问题