Java Output Formatting

来源:互联网 发布:元级潜艇模型 淘宝 编辑:程序博客网 时间:2024/06/10 05:18
printf的格式控制的完整格式: 
%  -  0  m.n  l或h  格式字符 
下面对组成格式说明的各项加以说明: 
①%:表示格式说明的起始符号,不可缺少。 
②-:有-表示左对齐输出,如省略表示右对齐输出。 
③0:有0表示指定空位填0,如省略表示指定空位不填。 
④m.n:m指域宽,即对应的输出项在输出设备上所占的字符数。N指精度。用于说明输出的实型数的小数位数。为指定n时,隐含的精度为n=6位。 

⑤l或h:l对整型指long型,对实型指double型。h用于将整型的格式字符修正为short型。 


Sample Input

java 100cpp 65python 50

Sample Output

================================java           100 cpp            065 python         050 ================================

Explanation

Each String is left-justified with trailing whitespace through the first  characters. The leading digit of the integer is the  character, and each integer that was less than  digits now has leading zeroes.


import java.util.Scanner;


public class Solution {


    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++)
            {
                String s1=sc.next();
                int x=sc.nextInt();
                //Complete this line
                System.out.printf("%-15s", s1);
                System.out.printf("%03d", x);
                System.out.println();
            }
            System.out.println("================================");


    }
}



0 0