编译时,我似乎得到一个“错误: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();
我假设我错过了一些完全明显的东西,我只是在旋转我所在的地方。任何帮助将不胜感激!
答案 0 :(得分:1)
您已将gamesPitched
声明为int
值的数组,但这不是您所需要的。根据要求,看起来应该像这样声明:
private NetID_BaseballGame[] gamesPitched = new NetID_BaseballGame[MAX_GAMES];
您还需要在某处初始化count
变量。