为什么下面的代码不适用于这里的问题? http://codingbat.com/prob/p101475初学者字符串操作
public String frontTimes(String str, int n) {
if (str.length() < 3) {
String front = str;
} else {
String front = str.substring(0, 3);
}
String copies = "";
while (n > 0) {
copies = copies + front;
n = n - 1;
}
return copies;
}
我真的不明白“前不能得到解决”的错误我得到
因为你定义了'if'内'字符串前...'声明,Java会在它离开该代码块时立即销毁它。所有你需要做的就是把'String front =“”;'放在'if'之前的一行。 – Jon 2015-01-20 18:44:51