(HakerRank) task是我格式化测试用例给出的输入。
字符串和整数之间需要有15个空格,如果只有两个数字,则在整数前面加一个零,我的代码尽我所知完成了这一点并匹配了预期的输出,但我的代码仍然被认为是错误的。
我可以帮忙找出原因吗?
我的代码:
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();
int length = String.valueOf(x).length();
if(length < 3) {
System.out.format("%-15s %03d %n", s1, x );
} else {
System.out.format("%-15s %d %n", s1, x );
}
}
System.out.println("================================");
}
}
只需删除字符串格式的空格,如下所示:
// ...
if(length < 3) {
System.out.format("%-15s%03d%n", s1, x);
} else {
System.out.format("%-15s%d%n", s1, x);
}
// ...
它通过了所有的测试:)