首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Main方法不会编译,无法找到创建对象的符号

Main方法不会编译,无法找到创建对象的符号
EN

Stack Overflow用户
提问于 2013-08-23 10:51:08
回答 6查看 9.2K关注 0票数 1

我对java很陌生,所以我想这是一个非常简单的问题,但我找不到答案

我正在创建一个非常简单的游戏,但是当我编译我的主要内容时,我得到

代码语言:javascript
复制
 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

因此,当我主要创建我的对象时,它无法找到符号并创建对象。

我的战舰级:

代码语言:javascript
复制
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;
}

}

主要方法:

代码语言:javascript
复制
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);

    }
 }

如果有人能让我知道为什么我不能创建一个对象?

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-08-23 10:55:13

你需要确保有两件事:

  1. 在使用BattleShip游戏之前,您应该先编译您的BattleShipGame类。
  2. 如果BattleShipBattleShipGame类都不在同一个package中,则需要使用导入语句在BattleShipGame类中导入BattleShip类。
票数 2
EN

Stack Overflow用户

发布于 2013-08-23 10:54:39

进口错过了。

代码语言:javascript
复制
   //imports missing here

public class BattleShipGame {
    public static void main(String[] args) {

        //create a battle ship object 
        BattleShip ship = new BattleShip();   

要使用该对象,必须使用import

我建议您使用IDE来纠正编译时错误并节省大量时间。

票数 0
EN

Stack Overflow用户

发布于 2013-08-23 10:56:19

您必须将这两个文件放在一个目录中。假设您将两个文件放在不同的目录中,比如GameMain。然后在主文件中,在启动import Game.BattleShip;时添加此行,并在第一行package Game;中的BattleShip类文件中添加这一行

这会解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18401019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档