作业的最后一道题不知道哪里错了哇,求大佬帮忙斧正一下。。
代码如下
/**
* Returns the string formed by alternating the case of the characters in
* the specified string. The first character in the returned string is in
* lowercase, the second character is in uppercase, the third character is
* in lowercase, the fourth character is in uppercase, and so on.
* Examples:
*
*
*alternatingCaps(“a”)
returns“a”
*alternatingCaps(“ab”)
returns“aB”
*alternatingCaps(“abc”)
returns“aBc”
*alternatingCaps(“XYZ”)
returns“xYz”
*alternatingCaps(“Toronto”)
returns“tOrOnTo”
*alternatingCaps(“eecs2030”)
returns“eEcS2030”
*
*
*
* The conversion of characters to lower or uppercase is identical to
* that performed by the methods Character.toLowerCase(int)
* and Character.toLowerCase(int)
*
* @param s
* a string
* @return the string formed by alternating the case of the characters in s
*/
public static String alternatingCaps(String s) {
String str = "" ; //从这里开始是我写的代码
for(int i = 0; i <= s.length()-1; i++) {
if(i % 2 == 0) {
str = s.toLowerCase();
}
else if(i % 2 != 0){
str = s.toUpperCase();
}
}
return str;
}
是很幼稚的问题,我想知道如何去锁定String里双数位置的字符,以及我的问题出现在哪里。
第一次在CSDN上面发布,格式用的不太好请各位大佬见谅。