我无法执行这行代码,请帮忙并修正?

huangapple go评论59阅读模式
英文:

I am unable to execute this line of code , Pls help and rectify?

问题

final int size = 10;
int[] crr = new int[size];
crr[0] = -1;
crr[1] = 2;
crr[2] = 6;
crr[3] = 9;

Scanner reader = new Scanner(System.in);

for (int i = 0; i < crr.length; i++);
{
System.out.println("The next element");
crr[i] = reader.nextInt();
System.out.println(crr[i]);
}


**执行后报错如下:**
&gt; 异常线程 "main" java.lang.Error: 未解析的编译问题
&gt; problems:  	I 无法解析为变量 	I 无法解析为变量

如果将i放在crr的位置上会显示为问题。请给予一些建议。

<details>
<summary>英文:</summary>

    final int size = 10 ;	
    	int [] crr = new int [size];
    	crr [0] = -1 ;
    	crr [1] = 2 ;
    	crr [2] = 6 ;
    	crr [3] = 9 ;
    	
    	Scanner reader = new Scanner(System.in);
    	
    	for (int i = 0 ; i &lt; crr.length ; i++ );
    	{
    		System.out.println (&quot;The next element&quot;);
             crr [i]= reader .nextInt ();
            System.out.println (crr[i]);

    
    

**After Execution Says this :**
&gt; Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation
&gt; problems:  	I cannot be resolved to a variable 	I cannot be resolved
&gt; to a variable

If I put i in the crr position it shows as its a problem. Please I need some advice on this.

If not copy the code and see the issue.

</details>


# 答案1
**得分**: 3

你用一个分号 `;` 来结束你的 `for` 循环块,因此你的循环被关闭,循环变量 `i` 的作用域也结束了。正确的循环应该如下所示:

```java
for (int i = 0; i < crr.length; i++) {
    System.out.println("The next element");
    crr[i] = reader.nextInt();
    System.out.println(crr[i]);
}

也许你应该查阅Java代码约定,这本文档还会告诉你如何格式化你的代码。
此外,如果你将来有任何问题,请提供一个最小可复制示例

英文:

You are closing your for loop block with a ;. Therefore your loop is closed and also the scope of your loop variable i ends.
The correct loop would look like:

for (int i = 0; i &lt; crr.length; i++) {
    System.out.println(&quot;The next element&quot;);
    crr[i] = reader.nextInt();
    System.out.println(crr[i]);
}

You should maybe look into the Java Code Conventions which also tell you more about how to format your code.
Also, if you ask any questions in the future, you should provide a Minimal, reproducible example.

huangapple
  • 本文由 发表于 2020年8月11日 14:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352846.html
匿名

发表评论

匿名网友

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

确定