0

我想创建一个程序,给定一对字符串,根据对中提供的长度限制它们或添加额外的字符。例如,它应该表现为:

 create (new Line[] {"try","chicken"},
 new Couple[] {new Couple(’t’,3),new Couple(’c’,3)})

应该返回

{"try", "chi"}

以上是当长度(指定的数字)小于或等于给定的单词时。下面是一个例子,如果长度大于单词。

create (new Line[] {"try","chicken"},
new Couple[] {new Couple(’t’,3),new Couple(’c’,9)})

应该返回

{"try", "chickenPP"}

也就是说,我们用字母 P 替换剩余的空格。

这是我尝试过的:

import java.util.Arrays;

public class Learn {

    private char firstChar;
    private int length;
    private char chars;
    private char FILL_CHARACTER = 'P';

    public Learn(char firstChar, int length, char chars) {
        this.length = length;
        this.firstChar = firstChar;
        this.chars = chars;
    }

    public static String[] create(String[] first, Couple[] second) {
        String[] strings = new String[first.length];
        for (int i = 0; i < first.length; i++) {
            if (second.length = first.chars) {
               learns[i] = first[i];
            } else if (second.length > first.chars) {
                   learns[i] = first[i] + FILL_CHARACTER;
            }
        }
    }
}

如您所见,我有很多错误。我想我可能对 API 感到困惑。请帮我改正。

4

1 回答 1

0

我不确定你在用Learnand做什么,但你可以通过sCouple来做这一切:String

public static String[] create(String[] words,int[] lengths) {
    char fill = 'P';
    String[] ret = new String[words.length];
    for ( int i = 0 ; i < ret.length ; i++ ) {
    ret[i] = words[i];
    while ( ret[i].length() < lengths[i] ) {
        ret[i] += fill;
    }
    ret[i] = ret[i].substring(0,lengths[i]);
    }
    return ret;
}

利用:
create(new String[]{"try","chicken"},new int[]{3,3});

于 2013-08-09T00:44:36.383 回答