Java Can';t分配给数组中的第一个元素

Java Can';t分配给数组中的第一个元素,java,arrays,assign,Java,Arrays,Assign,由于某些原因,我无法将随机数分配给我创建的int数组中的第一个元素。问题在第7行:coord[0]=(int)(math.random()*numRows+1)我在下面发布了错误 public class Ship { int shipLength = 3; int numRows = 5; int[] coord = new int[shipLength]; coord[0] = (int) (math.random() * numRows + 1); for (int i=1;

由于某些原因,我无法将随机数分配给我创建的int数组中的第一个元素。问题在第7行:
coord[0]=(int)(math.random()*numRows+1)我在下面发布了错误

public class Ship {

int shipLength = 3;
int numRows = 5;

int[] coord = new int[shipLength];
coord[0] = (int) (math.random() * numRows + 1);

    for (int i=1;i<shipLength;i++){
        coord[i] = coord[i-1] + 1;
    }
    public setCoord(cell){
        coord[cell] = null;
    }

    public int[] getCoord(cell){
    return coord[[cell];
    }
} //class




C:\java\Battleship>javac Ship.java
Ship.java:7: ']' expected
coord[0] = (int) (math.random() * numRows + 1);
      ^
Ship.java:7: ';' expected
coord[0] = (int) (math.random() * numRows + 1);
       ^
Ship.java:7: illegal start of type
coord[0] = (int) (math.random() * numRows + 1);
         ^
Ship.java:7: <identifier> expected
coord[0] = (int) (math.random() * numRows + 1);
          ^
公共级船舶{
int shipLength=3;
int numRows=5;
int[]坐标=新int[shipLength];
坐标[0]=(int)(math.random()*numRows+1);
对于(int i=1;ijavac Ship.java)
Ship.java:应为7:']'
坐标[0]=(int)(math.random()*numRows+1);
^
java:7:“;”应为
坐标[0]=(int)(math.random()*numRows+1);
^
java:7:类型的非法开始
坐标[0]=(int)(math.random()*numRows+1);
^
Ship.java:7:应为
坐标[0]=(int)(math.random()*numRows+1);
^

代码失败的特定行是有效的代码行,但它必须位于类的方法或构造函数中

例如:

public class Ship {

    int shipLength = 3;
    int numRows = 5;

    int[] coord = new int[shipLength];
    public Ship() {
        coord[0] = (int) (Math.random() * numRows + 1);
        for (int i=1;i<shipLength;i++){
            coord[i] = coord[i-1] + 1;
        }
    }
    public void setCoord(int cell, int value){
        coord[cell] = value;
    }

    public int getCoord(int cell){
        return coord[cell];
    }
} 
公共级船舶{
int shipLength=3;
int numRows=5;
int[]坐标=新int[shipLength];
公共船舶(){
坐标[0]=(int)(Math.random()*numRows+1);

对于(inti=1;i您在
公共类Ship{
之后和
公共setCoord(cell){
之前拥有的代码都是类级别的,但它是必须在构造函数、方法或实例初始值设定项中的逐步代码

还有其他几个基本错误

也许:

public class Ship {
    int shipLength = 3;
    int numRows = 5;
    int[] coord;

    public Ship() {

        coord = new int[shipLength];
        coord[0] = (int) (Math.random() * numRows + 1);

        for (int i=1;i<shipLength;i++){
            coord[i] = coord[i-1] + 1;
        }
    }

    public void setCoord(int cell){
        coord[cell] = 0;
    }

    public int getCoord(int cell){
        return coord[cell];
    }
} //class
公共级船舶{
int shipLength=3;
int numRows=5;
国际[]合作;
公共船舶(){
坐标=新整数[船长];
坐标[0]=(int)(Math.random()*numRows+1);

对于(int i=1;i你认为代码应该在什么时候执行?类主体是用来做什么的?方法在哪里?你的代码结构是错误的我建议你看一个基本的java教程。这是一个很好的教程:谢谢你的推荐,juan,我现在实际上正在做一个教程,但速度有点慢,所以我尝试了超越我的水平在练习中问我自己的理智和娱乐。@Sotirios这是一个战舰游戏程序。这是一个在虚拟平面上随机坐标创建战舰的类。
getCoord
方法应该返回一个int@user184994:这还不是全部,代码中充满了错误。是的:)我刚刚在另一篇评论中提到了同样的问题,它仍然不会编译——它到处都是错误。
setCoord
方法没有返回类型,
getCoord
方法应该返回一个int,而且它有一个太多的
[
@user184994抱歉,我做了一个更新,我的第一次迭代只是将东西放入构造函数中。仍然需要更改
getCoord
的返回类型。
coord
变量是一个int数组,因此
coord[cell]
将返回一个int:)@是的,user184994正在用肉眼进行更正,因为我的IDE还没有加载。啊,好的,所以当你做任何事情时,除了将实例变量分配给文本外,构造函数都是必要的。这很有意义。谢谢你的帮助。