繁体   English   中英

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

[英]java system.out.printf format numerics rounding off and spacing issue together

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 

您获得的代码与您显示的输出不匹配 - 它已经接近正确了。 它提出了:

java  100 
cpp   065 
python 060

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

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

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

    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.
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("================================");

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

我不知道为什么,但删除 "" 中的空格有效。

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("================================"); } }
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("================================");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM