如何返回字符大小写交替形成的字符串?

该方法应该返回通过交替指定字符串中的字符大小写形成的字符串。返回字符串中的第一个字符为小写,第二个字符为大写,第三个字符为小写,第四个字符为大写,依此类推。示例:tOrOnTo。


public static String alternatingCaps(String s) {

    for (int i = 0; i < s.length(); i++) {

        if (i % 2 == 0) {

            System.out.print(Character.toUpperCase(s.charAt(i)));

        } else {

            System.out.print(Character.toLowerCase(s.charAt(i)));

        }

    }

    return s;

}

我希望它能起作用,但它一直未能通过 JUnit 测试。


ABOUTYOU
浏览 172回答 5
5回答

互换的青春

IDK,为什么所有其他解决方案都错过了一个必要条件(第一个字符必须是小写,0%2 == 0)。您的测试代码仍然会失败。正确的解决方案是,&nbsp;public static String alternatingCaps(String s) {&nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; for (int i = 0; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; result.append(Character.toLowerCase(s.charAt(i)));&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; result.append(Character.toUpperCase(s.charAt(i)));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result.toString();&nbsp;}PS:交换方法调用toUpperCase和toLowerCase其他答案以获得正确的解决方案。

潇湘沐

Java 中的字符串是不可变的,因此您无法修改它。所有修改字符串的方法都会在内部创建一个新字符串并返回它。我认为最好的解决方案是使用StringBuilder:public static String alternatingCaps(String str) {&nbsp; &nbsp; StringBuilder buf = new StringBuilder(str.length());&nbsp; &nbsp; for (int i = 0; i < str.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; buf.append(i % 2 == 0 ? Character.toLowerCase(ch) : Character.toUpperCase(ch));&nbsp; &nbsp; }&nbsp; &nbsp; return buf.toString();}

慕斯王

您可以使用数组。public class Main{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String s = alternatingCaps("toronto");&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(s);&nbsp; &nbsp; }&nbsp; &nbsp; public static String alternatingCaps(String s) {&nbsp; &nbsp; &nbsp; &nbsp; char [] array = new char[s.length()];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i]=Character.toUpperCase(s.charAt(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i]=Character.toLowerCase(s.charAt(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return String.valueOf(array);&nbsp;&nbsp; &nbsp; }}

红糖糍粑

正如其他人所说,您实际上并没有更改返回的值,而只是打印修改后的值。我采取了另一种方法来演示另一种在不使用模的情况下更改每个其他值的方法:public static String alternatingCaps(String s) {&nbsp; &nbsp; boolean toggle = true;&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; for (char c : s.toCharArray()) {&nbsp; &nbsp; &nbsp; &nbsp; if (toggle = !toggle) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(Character.toUpperCase(c));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(Character.toLowerCase(c));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return sb.toString();}这使用StringBuilder来构建返回String,并使用String.toCharArray()增强for循环来迭代所有字符而不是索引。我们能够for在这里使用增强循环,因为我们改变了确定索引是奇数还是偶数的方式。我们不使用模数,而是使用在每次传递之间切换的值,从而有效地执行相同的操作boolean。truefalse请注意,此方法总体上不如模方法,因为如果您想要每第三个值等,则可以轻松修改模,并且比此方法更容易阅读。这个例子主要是为了学习其他解决问题的方法。我还避免了三元运算符,因此更容易看到切换是如何boolean工作的,尽管可以通过使用它来进一步简化。

子衿沉夜

您不会返回已处理的String,您只是将其打印在 StdOut 上,因此输入String不会更改并且您的 jUnit 会失败。这里使用 a StringBuilder,追加处理后的字符,最后String通过调用toString()上的方法返回StringBuilder。public static String alternatingCaps(String s) {&nbsp; &nbsp; StringBuilder temp = new StringBuilder();&nbsp; &nbsp; IntStream.range(0, s.length()).forEach(idx ->{&nbsp; &nbsp; &nbsp; &nbsp; if (idx % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.append(Character.toUpperCase(s.charAt(idx)));&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.append(Character.toLowerCase(s.charAt(idx)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return temp.toString();}另一种使用Collectors#joiningandIntStream迭代输入索引的方式String应用逻辑并使用以下方式收集它Collectors#joining:public static String alternatingCaps(String s) {&nbsp; &nbsp; return IntStream.range(0, s.length())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToObj(idx -> String.valueOf(idx % 2 == 0&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;? Character.toUpperCase(s.charAt(idx))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Character.toLowerCase(s.charAt(idx))))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.joining(""));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java