Note:这些内容都是来自hackerrank的题目笔记和讨论区。
先看这道题的解法:
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(); //get strings
int x=sc.nextInt(); //get integers
//Complete this line
System.out.printf("%-15s%03d%n",s1,x);
}
System.out.println("================================");
}
}
题目要求: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.
每行都得要15个characters,数字最多三位,不够的在前面补充0
这是一种解法:
System.out.printf(“%-15s%03d %n”,s1,x);
Explanations: %-15s : left justify 15: Total 15 characters of strig right from begining s: for string
%03d
03: Will pad 0 to left if number is less than 3 digit d: for integer
%n : for new line
补充的格式输出:
http://www.cnblogs.com/huhx/p/javaFormatter.html#formatter_introduce

本文解析了一道Hackerrank编程题目,并详细介绍了使用Java进行字符串和整数格式化输出的方法。通过具体代码示例展示了如何实现左对齐的字符串输出以及整数的前导零填充,确保输出格式符合题目要求。
5万+

被折叠的 条评论
为什么被折叠?



