我对java很陌生,所以我想这是一个非常简单的问题,但我找不到答案
我正在创建一个非常简单的游戏,但是当我编译我的主要内容时,我得到
BattleShipGame.java:19: error: cannot find symbol
BattleShip ship = new BattleShip();
^
symbol: class BattleShip
location: class BattleShipGame
BattleShipGame.java:19: error: cannot find symbol
BattleShip ship = new BattleShip();
^
symbol: class BattleShip
location: class BattleShipGame
2 errors因此,当我主要创建我的对象时,它无法找到符号并创建对象。
我的战舰级:
public class BattleShip {
//delcare an int arry to hold the location of the cells
private int[] location;
//setter for location
public void setLocation(int[] shipLocation){
location = shipLocation;
}
public String checkGuess(String[] g){
//return the message
return message;
}
}主要方法:
public class BattleShipGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create a battle ship object
BattleShip ship = new BattleShip();
//hard code location of ship
int[] ShipLocation = {4,5,6};
//set the location of the object
ship.setLocation(ShipLocation);
//take the users guess from command line
String[] guess = {args[0], args[1], args[2]};
//take message returned from method
String message = ship.checkGuess(guess);
// print out the message
System.out.println(message);
}
}如果有人能让我知道为什么我不能创建一个对象?
我编译了战舰类之前,主要--它们都在同一个包里,我还需要导入吗?
发布于 2013-08-23 10:55:13
你需要确保有两件事:
BattleShip游戏之前,您应该先编译您的BattleShipGame类。BattleShip和BattleShipGame类都不在同一个package中,则需要使用导入语句在BattleShipGame类中导入BattleShip类。发布于 2013-08-23 10:54:39
进口错过了。
//imports missing here
public class BattleShipGame {
public static void main(String[] args) {
//create a battle ship object
BattleShip ship = new BattleShip(); 要使用该对象,必须使用import
我建议您使用IDE来纠正编译时错误并节省大量时间。
发布于 2013-08-23 10:56:19
您必须将这两个文件放在一个目录中。假设您将两个文件放在不同的目录中,比如Game和Main。然后在主文件中,在启动import Game.BattleShip;时添加此行,并在第一行package Game;中的BattleShip类文件中添加这一行
这会解决你的问题。
https://stackoverflow.com/questions/18401019
复制相似问题