SimpleTocom java错误

SimpleTocom java错误,java,Java,我很快就要发疯了。我已经尽我所能了! 问题:当我尝试在命令行中运行我的程序时,我遇到以下错误: 错误:找不到符号 SimpleTocom点=新的SimpleTocom(); ^ ^ 符号:SimpleTocom类 位置:SimpleToCommTestDrive类 代码如下: public class SimpleDotComTestDrive { public static void main (String[] args) {

我很快就要发疯了。我已经尽我所能了! 问题:当我尝试在命令行中运行我的程序时,我遇到以下错误:

错误:找不到符号 SimpleTocom点=新的SimpleTocom(); ^ ^ 符号:SimpleTocom类 位置:SimpleToCommTestDrive类

代码如下:

public class SimpleDotComTestDrive {

    public static void main (String[] args) {

        SimpleDotCom dot = new SimpleDotCom();

        int[] locations = {2, 3 ,4};

        dot.setLocationCells(locations);

        String userGuess = "2";

        String result = dot.checkYourself(userGuess);

    }

public class SimpleDotCom {

    int[] locationCells;
    int numOfHits = 0;

    public void setLocationCells(int[] locs) {
        locationCells = locs;   
    }

    public String checkYourself(String stringGuess) {

        int guess = Integer.parseInt(stringGuess);
        String result = "miss";
        for (int cell : locationCells) {

            if (guess == cell) {

                result = "hit";
                numOfHits++;
                break;

            }
        }   
        if (numOfHits == locationCells.length) {

            result = "kill";

        }

        System.out.println(result);
        return result;



   }

}
声明

SimpleDotCom dot = new SimpleDotCom(); 
失败,因为在没有封闭实例的情况下无法实例化内部类。相反,你可以写作

SimpleDotCom dot = (new SimpleDotComTestDrive()).new SimpleDotCom();

它将提供一个内联的封闭实例。或者,只需将类
simpledocom
设置为静态,这意味着它不会有封闭实例。

如果simpledocom是一个内部类,则需要将其“分配”给外部类的实例

SimpleDotComTestDrive outer = new SimpleDotComTestDrive();
outer.SimpleDotCom dot = outer.new SimpleDotCom();
但是,这在静态环境中仍然不起作用,因为
main


另一个选项是使内部类
静态
,并通过
SimpleToComTestDrive访问它。SimpleTocom

您需要将这两个类保存在不同的类文件中。这段代码在eclipse和命令行编译器中都能正常工作

这就是你必须做的。1) 创建一个名为SimpleToComTestDrive的类,并将这部分代码放入其中

public class SimpleDotComTestDrive {

    public static void main (String[] args) {

        SimpleDotCom dot = new SimpleDotCom();

        int[] locations = {2, 3 ,4};

        dot.setLocationCells(locations);

        String userGuess = "2";

        String result = dot.checkYourself(userGuess);

    }
}
然后创建另一个名为SimpleTocom的类并粘贴此代码

public class SimpleDotCom {

    int[] locationCells;
    int numOfHits = 0;

void setLocationCells(int[] locs) {
        locationCells = locs;   
    }

    String checkYourself(String stringGuess) {

        int guess = Integer.parseInt(stringGuess);
        String result = "miss";
        for (int cell : locationCells) {

            if (guess == cell) {

                result = "hit";
                numOfHits++;
                break;

            }
        }   
        if (numOfHits == locationCells.length) {

            result = "kill";

        }

        System.out.println(result);
        return result;



   }

}

尝试将
simpledocom
定义为
static
类。您试图从静态上下文中使用它,但目前它不是静态的。一般来说,最好将每个类放在一个新文件中。我认为这不正确。正如我的评论和@Reimeus的回答中提到的,该类必须是静态的,以便可以在static
main
方法中访问它。