remove unwanted consecutive char set if in argument(String) and return the filtered argument, else return original argument string

huangapple 未分类评论47阅读模式
英文:

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(&quot;&quot;);
  String[] newsarr = new String[sarr.length-3];
  String s = &quot;&quot;;
  for (int i = 1; i &lt; sarr.length; i++); {
    if ((sarr[i] == &quot;a&quot;) &amp;&amp; (sarr.length &gt;= i+3)) {
  if ((sarr[i+1] == &quot;b&quot;) &amp;&amp; (sarr[i+2] == &quot;c&quot;)) {
    newsarr[0] = sarr[0];
    for (int x = 1; x &lt; i; x++) {
      newsarr[i] = sarr[i];
    }
    for (int y = i+3; y &lt; 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.

huangapple
  • 本文由 发表于 2020年5月5日 01:57:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61598637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定