为什么,当我使用下面的操作来计算字符时,它是否返回数字而不是字符?它不应该给出相同的结果吗?
ret += ... ; // returns numbers
ret = ret + ...; // returns chars
Run Code Online (Sandbox Code Playgroud)
下面的代码重复了字符:
doubleChar("The")→"TThhee"
public String doubleChar(String str) {
String ret = "";
for(int i = 0; i < str.length(); i++) {
ret = ret + str.charAt(i) + str.charAt(i); // it concatenates the letters correctly
//ret += str.charAt(i) + str.charAt(i); // it concatenates numbers
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)