gpt4 book ai didi

java - Main 方法无法编译找不到符号来创建对象

转载 作者:行者123 更新时间:2023-12-01 14:12:23 25 4
gpt4 key购买 nike

我是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);

}
}

如果有人能让我知道为什么我无法创建对象?

我在主程序之前编译了战舰类它们都在同一个包中,我还需要导入吗?

最佳答案

您需要确定两件事:

  1. 您应该先编译 BattleShip 类,然后再在 BattleShipGame 游戏中使用它
  2. 如果 BattleShipBattleShipGame 类不在同一个 package 中,那么您需要导入 BattleShip使用 import 语句在 BattleShipGame 类中使用 import 语句。

关于java - Main 方法无法编译找不到符号来创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18401019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com