英文:
remove unwanted consecutive char set if in argument(String) and return the filtered argument, else return original argument string
问题
代码中的问题是:"'i' cannot be resolved to a variable",是否可以解释一下??
static String abc(String str) {
String[] sarr = str.split("");
String[] newsarr = new String[sarr.length - 3];
String s = "";
for (int i = 1; i < sarr.length; i++) ;
{
if ((sarr[i] == "a") && (sarr.length >= i + 3)) {
if ((sarr[i + 1] == "b") && (sarr[i + 2] == "c")) {
newsarr[0] = sarr[0];
for (int x = 1; x < i; x++) {
newsarr[i] = sarr[i];
}
for (int y = i + 3; y < newsarr.length; y++) {
newsarr[y - 3] = sarr[y];
}
} else {
}
} else {
}
}
for (String o : newsarr) {
s += o;
}
return s;
}
英文:
the code below throws the error " 'i' cannot be resolved to a variable ", any explanations please??
static String abc(String str) {
String[] sarr = str.split("");
String[] newsarr = new String[sarr.length-3];
String s = "";
for (int i = 1; i < sarr.length; i++); {
if ((sarr[i] == "a") && (sarr.length >= i+3)) {
if ((sarr[i+1] == "b") && (sarr[i+2] == "c")) {
newsarr[0] = sarr[0];
for (int x = 1; x < i; x++) {
newsarr[i] = sarr[i];
}
for (int y = i+3; y < newsarr.length; y++) {
newsarr[y-3] = sarr[y];
}
} else {}
} else {}
}
for (String o : newsarr) {
s += o;
}
return s;
}
答案1
得分: -1
删除第一个for循环上的分号将会产生奇妙的效果。
分号终止了for语句以及变量i的作用域。
英文:
Removing the semicolon on the first for loop will work wonders.
The semicolon ends the for statement and the scope of i.
专注分享java语言的经验与见解,让所有开发者获益!
评论