【发布时间】:2014-06-05 18:39:08
【问题描述】:
由于某种原因,我无法为我创建的 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);
^
【问题讨论】:
-
你认为代码应该在什么时候执行?类体有什么用?
-
方法在哪里??你的代码结构不对
-
我推荐你看一个基本的java教程。这是一个很好的教程:docs.oracle.com/javase/tutorial/java/index.html
-
感谢 juan 的推荐,我现在实际上正在做一个教程,但速度有点慢,所以为了我自己的理智和娱乐,我试着超出了我在练习中被要求的内容。
-
@Sotirios 这是一个战舰游戏程序。这是在虚拟平面中以随机坐标创建战舰的类。