gpt4 book ai didi

java - 错误: int cannot be dereferenced

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

编译时我似乎收到“错误: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/

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