【问题标题】:How to align the output in java from right?如何从右对齐java中的输出?
【发布时间】:2013-10-12 05:08:21
【问题描述】:

我想在java中对齐我的输出如下:

   1234
  *3406
----------------
   7404
     0
 4936
3702
-----------------
4201004

【问题讨论】:

  • 在哪个介质中对齐?控制台输出? HTML?摇摆?

标签: java alignment output


【解决方案1】:

使用System.out.printf,如下所示:

public class Multi {
    public static void main(String[] args) {
        int align=10;
        int a=1234;
        int b=3406;
         System.out.printf("%"+align+"d\n",a);
         System.out.printf("%"+align+"d\n",b);
         System.out.println("---------------------------");
        int res=a*b;
        while(b!=0){
            int t=b%10;            
            System.out.printf("%"+(align--)+"d\n",t*a);
            b/=10;
        }
        System.out.println("---------------------------");
        System.out.printf("%10d\n",res);

    }
}

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 2014-05-18
      • 2017-09-19
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2018-03-09
      相关资源
      最近更新 更多