java system.out.printf格式数字四舍五入和间距问题在一起

时间:2015-08-14 07:16:50

标签: java arrays core

public class Solution {    
    public static void main(String[] args) {
        String[] languages = { "java", "cpp", "python" };
        int[] values = { 100, 65, 60 };

        for (int i = 0; i < 3; i++) {
            System.out.printf("%-5s %03d %n", languages[i], values[i]);
            //Complete this line
        }
    }
}

输出我正在

java 100
cpp 65
python 50

期望的输出

java           100 
cpp            065 
python         050 

6 个答案:

答案 0 :(得分:1)

您获得的代码与您显示的输出不匹配 - 它已经几乎了。它提出了:

java  100 
cpp   065 
python 060

这里唯一的问题是你将名称填充到最少5个字符......但是python有6个字符。所以你只需要增加格式字符串中的5。例如:

System.out.printf("%-10s %03d %n", languages[i], values[i]);

另外,您的评论表明您不知道格式字符串的每个位是什么。您应该仔细阅读Formatter文档。例如,-中的%-10s是一个标志,其含义为“结果将是左对齐的。”

答案 1 :(得分:0)

    System.out.printf("%-15s %03d %n", s1, x);

其中s1是一个字符串,x是一个数字。

说明:

 in the above code -15s is defined as follows : - is for left justified, and width is specified as 15. s is to print the string
 in the above code  %03d is defined as follows.

    0 - to pad with zeros
    3 - width of the field was set as 3.

Running the program:
        ================================
        java 100(input)
        java           100
        cpp 65(input)
        cpp            065
        ================================

Now the second line output "100", starts at the 16th character/position.Also it is padding 0 in the 4th line output.

答案 2 :(得分:0)

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.print(s1 );
                System.out.print("\t\t");

                System.out.println(x);


            }
            System.out.println("================================");

    }
}

答案 3 :(得分:0)

System.out.printf("%-15s%03d%n", s1, x);

我不知道为什么,但是删除“”内的空格是可行的。

答案 4 :(得分:0)

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();
                System.out.printf("%-15s",s1);
                System.out.printf("%03d",x);
                System.out.println("");
            }
            System.out.println("================================");

    }
}

答案 5 :(得分:0)

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("%-14s %03d %n",s1,x);
            }
            System.out.println("================================");
    }
}