java - 错误: int cannot be dereferenced

标签 java compiler-errors int dereference primitive-types

编译时我似乎收到“错误:int 无法取消引用”错误。我已经查找了发生这种情况的原因,尽管如此,我不确定如何解决它。

public class NetID_BaseballPitcher
{
    Scanner in = new Scanner(System.in);
    private final int maxgames = 20;
    private int[] gamesPitched = new int[maxgames];
    private int count, totalRuns;
    private float totalInnings;

    public int inputGameData()
    {
        for(int i = 0; i < count; i++)
        {
            gamesPitched[i] = new NetID_BaseballGame();
            gamesPitched[i].inputGame();
        }
    }  
}

这是 Baseballgame.java

public class NetID_BaseballGame
{
    private static int runs;
    private static int gameNum;
    private static float innings;
    public static void inputGame()
    {
        do
        {
            System.out.print("Game # ");
            gameNum = in.nextInt();
        } while( gameNum < 0 );
        do
        {
            System.out.print("Number Innings");
            innings = in.nextInt();
        } while( ( innings < 0 ) && ( innings > 10 ) );

        do
        {
            System.out.print("Number Runs");
            runs = in.nextInt();
        } while( runs < 0 );
    }
}

编译 BaseballPitcher.java 时,我得到以下信息:

NetID_BaseballPitcher.java:45: error: incompatible types
            gamesPitched[i] = new NetID_BaseballGame();
                          ^
  required: int
  found:    NetID_BaseballGame
NetID_BaseballPitcher.java:46: error: int cannot be dereferenced
        gamesPitched[i].inputGame();

我假设我错过了一些完全明显的东西,而且我只是在原地打转。任何帮助将不胜感激!

最佳答案

您已将 gamesPitched 声明为 int 值的数组,但这不是您所需要的。从要求来看,您应该这样声明:

private NetID_BaseballGame[] gamesPitched = new NetID_BaseballGame[MAX_GAMES];

您还需要在某处初始化您的 count 变量。

关于java - 错误: int cannot be dereferenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18754406/

相关文章:

java - java中run()方法可以使用synchronized吗?

java - 测试 Spring 时创建 bean 时出错

java - 使用 Glassfish 和 CDI 获取服务属性或其他包

python-3.x - Python IF 语句给出 "invalid syntax"错误。请让我知道可能是什么原因

c++ - 编写if语句时出现 “' =' : left operand must be l-value”错误

java - 带有 jdbc 和散列密码的 shiro

linux - `gcc: error: 0": No such file or directory` error? 是什么性质

swift - 在 Int swift 中转换 Char

c# - 为什么 int 和 long 类型是一流的并且受到 C# 和运行时的青睐?

java - 整数到十六进制字符串 NumberformatException