我的功能是密码生成器。它选择句子中单词的数量。并交替取第一个字母和最后一个字母。示例在 void main 方法中。
我的问题是我不知道如何旋转 firstChar 和 lastChar。我在 if 语句中用模数尝试了它,但没有让我更进一步。也许你们有一些想法。
public static String pwdgen(String s) {
String[] Splitted = s.split(" ");
int count = s.split(" ").length;
int i = 0;
String firstChar = "";
String lastChar = "";
for(i = 0; i < count; i = i + 1) {
firstChar += Splitted[i].substring(0, 1);
lastChar += Splitted[i].substring(Splitted[i].length() - 1);
}
return count + firstChar + lastChar;
}
public static void main(String[] args) {
String pwd = pwdgen("Dies ist nur ein doofes Beispiel");
System.out.println(pwd); // => "6Dtnndl"
System.out.println(pwdgen("a b c")); // => 3abc
}