Java Main方法无法编译无法找到用于创建对象的符号
我是java新手,所以我想这是一个非常简单的问题,但我找不到答案 我正在创建一个非常简单的游戏,但当我来编译我的主要Java Main方法无法编译无法找到用于创建对象的符号,java,Java,我是java新手,所以我想这是一个非常简单的问题,但我找不到答案 我正在创建一个非常简单的游戏,但当我来编译我的主要 BattleShipGame.java:19: error: cannot find symbol BattleShip ship = new BattleShip(); ^ symbol: class BattleShip location: class BattleShipGame BattleShipGame.java:19: error:
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);
}
}
如果有人能告诉我为什么我不能创建一个对象
我在主战之前编译了战舰类
这些都在同一个包中,我还需要导入吗 导入丢失
//imports missing here
public class BattleShipGame {
public static void main(String[] args) {
//create a battle ship object
BattleShip ship = new BattleShip();
要使用该对象,必须导入
我建议您使用IDE来纠正编译时错误并节省大量时间您需要确保两件事:
你应该先编译你的战舰
类,然后再在战舰游戏
中使用它
如果BattleShip
和BattleShipGame
类不在同一个包中
,则需要使用导入语句在BattleShipGame
类中导入BattleShip
类
您忘记了在BattleShipGame.java
中导入com.your.package.BattleShip
例如,使用Eclipse或任何其他IDE,它们将为您管理导入。在Eclipse中,快捷键是Ctrl+Shift+O。您必须将两个文件放在一个目录中。假设您将这两个文件放在不同的目录中,例如
Game
和Main
。然后在主文件中,在startimport Game.BattleShip中添加这一行
并在第一行打包游戏的战列舰类文件中添加该行代码>
这将解决您的问题。在同一目录中编写booth类,或在战舰游戏
类中使用导入语句
import com.test.BattleShip;
public class BattleShipGame {
public static void main(String[] args) {
BattleShip ship = new BattleShip();
}
}
我不太确定是什么问题,我将文件从netbeans创建的项目中移出,一切都很好,谢谢你的回答,不过你是如何编译的?你导入了Battleship类吗?在BattleShipGame类中导入Battleship类。不建议使用带有Java导入语句的通配符@AdamStelmaszczyk感谢您的链接。(非常感谢。)鉴于我们所看到的情况,我怀疑没有编译战舰
是当前的问题。