简体   繁体   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
        }
    }
}

Output I'm getting我得到的输出

java 100
cpp 65
python 50

Desired output期望输出

java           100 
cpp            065 
python         050 

The code you've got doesn't match the output you've shown - it's nearly right already.您获得的代码与您显示的输出不匹配 - 它已经接近正确了。 It comes up with:它提出了:

java  100 
cpp   065 
python 060

The only problem here is that you're right-padding the names to a minimum of 5 characters... but python has 6 characters.这里唯一的问题是您将名称右填充为至少 5 个字符……但是 python 有 6 个字符。 So you just need to increase the 5 in your format string.所以你只需要在你的格式字符串中增加5 For example:例如:

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

Separately, your comments have suggested that you don't know what each of the bits of your format string does.另外,您的评论表明您不知道格式字符串的每个位的作用。 You should read the Formatter documentation carefully.您应该仔细阅读Formatter文档。 For example, the - in %-10s is a flag, with a meaning of "The result will be left-justified."例如, %-10s-是一个标志,意思是“结果将左对齐”。

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

where s1 is a string and x is a number.其中 s1 是一个字符串,x 是一个数字。

Explanation:解释:

 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