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
新闻热点
疑难解答