簡體   English   中英

Hackerrank不接受有效的解決方案

[英]Hackerrank not accepting valid solution

(HakerRank)任務是我格式化測試用例給出的輸入。

字符串和整數之間需要有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);
}
// ...

它通過了所有的測試:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM