编译器将int视为一个类

时间:2016-09-02 03:30:55

标签: java

我正在使用Netbeans IDE,我面临一个小错误,这会破坏一切。正如标题所暗示的,这里的编译器假设变量' i'是一个类而不是整数,即使我已经将它声明为整数

public class PlayerOceanGrid extends javax.swing.JFrame {

boolean play = false;
boolean deploy = false;
boolean vertical = true; 
boolean horizontal = false;
boolean deploycarrier, deploycruiser, deploysubmarine, deploydestroyer, deploybattleship = false;
boolean place = false;
**int i = 0;**
int [] [] coordinate = new int [10] [10];
int randomr = (int) (Math.random() * 10); 
int randomc = (int) (Math.random() * 10); 
String [] shiptype = {"submarine", "cruiser", "carrier", "destroyer", "battleship"};
/* int randomtype = (int) (Math.random() * 5);
String finaltype = shiptype [randomtype]; 
*/
char [] facing = {'H', 'V'};
/* int randomfacing = (int) (Math.random() * 2);
char finalfacing = facing [randomfacing]; 
*/
int [][] coordinates = new int [10][10];

for (i = 0; i < 100; i++)
{

}

编译器说

非法启动类型

无法找到符号    符号:第一类    location:PlayerOceanGrid

预期的标识符

它建议我创建一个名为i的课程,这不是我想要的。 (我希望&#39;我在for循环中用作整数!)

3 个答案:

答案 0 :(得分:1)

真正的问题是你的for循环;它不在方法,构造函数或初始化程序块中。所以这是非法表达。

void myMethod() {
    for (i = 0; i < 100; i++)
    {

    }
}

但是,除此之外,您的循环 for 还不清楚。

答案 1 :(得分:0)

您确定编译指向您指定的行吗?

你有一个for循环坐在“类中”(不在方法内),这绝对不合法。

答案 2 :(得分:0)

问题不是变量i,而是for循环。在课堂上编写for循环是非法的,尝试用方法编写。

相关问题