1

我正在做一个作业,它需要一个带有字符串和 int 作为参数的方法。该方法应该用空格填充参数字符串,直到它的长度是给定的长度。例如,padString("hello", 8 应该返回 "hello___"(_ 代表三个空格)) 如果 int 大于字符串的长度,那么它会简单地返回字符串。我无法关闭程序的“填充”部分。

由于这个任务在本书的早期阶段,我假设它可以用初学者的东西来完成,比如 forloops、参数和常见的字符串方法,因为我不应该使用 if/else 语句。

这是我目前拥有的明显有缺陷的代码:

public class Exercise11 {

public static final String word = "congratulations";
public static final int length = 10;

public static void main(String[] args) {
    padString();

}

public static String padString(String word, int length) {
    if (word.length() >= length) {
        return word.substring(0,length + 1);
    } else {
        String thing = word;
        return word + addSpaces(word, length);
    }

}

public static void addSpaces(String word, int length) {
    for (int i = 1; i <= length-word.length(); i++) {
            return (" ");
        }
}

}

顺便说一句,有没有办法使用 for 循环将空格等内容添加到 String 变量中?谢谢您的帮助。

4

4 回答 4

3

这是一个合理的开始...

padString期待两个参数, aStringint,所以这...

public static void main(String[] args) {
    padString();
}

可以改成...

public static void main(String[] args) {
    padString(word, length);
}

下一个问题在于你的addSpaces方法。循环中的return语句意味着循环只会执行一次,并且只会返回一个空格字符。

相反,您需要将每个循环上的空间连接到一个临时String的,您将传递回来,例如......

public static String addSpaces(String word, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length - word.length(); i++) {
        sb.append(" ");
    }
    return sb.toString();
}

所以,如果我跑...

System.out.println("[" + padString("hello", length) + "]");

我明白了

[你好 ]

于 2013-09-16T07:03:12.480 回答
1

直接的问题是,无论您需要添加多少空格,您的for循环都会立即返回一个空格。addSpaces更好的方法是addSpaces返回String并向内部String变量添加空格,直到它的长度正确,然后返回。

此外,在 Java 中,习惯从零开始循环,除非有特别充分的理由从其他地方开始。在您的情况下,word.length()可能是一个很好的起点,但 1 不是。

于 2013-09-16T07:00:19.677 回答
0

您的添加空间什么都不返回。这是void。内部逻辑也无法满足您的要求。

您的addSpaces 方法应该类似于

public static String addSpaces(String word, int length) {
     String spaces="";
    for (int i = 1; i <= length-word.length(); i++) {
           spaces +=  " ";
        }
     return  spaces;
} 

并且更喜欢使用StringBuilder附加字符串来获得更好的性能。

使用 StringBuilder

 public static String addSpaces(String word, int length) {
         StringBuilder spaces=new StringBuilder();
        for (int i = 1; i <= length-word.length(); i++) {
               StringBuilder.append(" ");
            }
         return  spaces.toString();
    } 

然后你就return word + addSpaces(word, length); 完美了。假设我没有错过任何其他东西:)。

于 2013-09-16T07:01:11.147 回答
0

谢谢大家,现在终于可以用了。

这是代码的最终副本:

public class Exercise11 {

public static final String WORD = "congratulations";
public static final int LENGTH = 10;

public static void main(String[] args) {
    System.out.println(padString(WORD, LENGTH));
}

private static String padString(String word, int length) {
    if (word.length() >= length) {
        return word.substring(0,word.length());
    } else {
        return word + addSpaces(word, length);
    }

}

public static String addSpaces(String word, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length - word.length(); i++) {
        sb.append("_"); // these are more visible than empty spaces
    }
    return sb.toString();
}

}

不过,我还有几个问题。构造“Stringbuilder”的意义何在?什么是“append”方法?

因为书上现在没有这些东西,有没有别的方法?

于 2013-09-16T07:23:22.047 回答