gpt4 book ai didi

Java ~ 返回数组时类型不兼容?

转载 作者:行者123 更新时间:2023-12-01 13:46:50 26 4
gpt4 key购买 nike

我已经在同一个项目中完成了(几乎)完全相同的数组返回,它编译了一个类(PlayerArray),但由于某种原因,当我尝试在我的( Planet) 类它给了我错误“不兼容类型”,但我无法理解为什么会这样,我的意思是该方法返回一个数组并且它确实返回一个数组?如果有人可以解释这些差异以及为什么一个有效而另一个提示“不兼容的类型”,那么那就太好了。谢谢!这是我的一些代码。

这是我的启动器。

public class Launcher
{
private static Planet myTest;
private static PlanetInfo myPlanetInfo;
private static PlanetInfo[] planetArray;
private static Player[] arr; //NEED THIS FOR arr = myArray.getPlayerArray(); to work..
public static void main(String[] args)
{
Planet myPlanetArray = new Planet();
PlayerArray myArray = new PlayerArray();
myPlanetArray.getPlanetArray();
myArray.getPlayerArray();
planetArray = myPlanetArray.returnCost();/////////////////////THIS IS THE BROKEN LINE
arr = myArray.getPlayerArray(); //holy moses this worked...
System.out.println("player 1's ID: " + arr[0].viewID());
System.out.println("player 1's balance: " + arr[0].viewBalance());
arr[0].playerGo();
}
}

下面的玩家数组可以工作..

public class PlayerArray
{
Scanner scan = new Scanner(System.in);
private int numbHuman;
private Player[] arr;
private String[] userName;
private int[] userID;
private int startingMoney;
private int startingPosition;
private int b;
public PlayerArray()
{
Scanner scan = new Scanner(System.in);
System.out.println("How many players wish to play? Values of 2 through 8 accepted.");
numbHuman = scan.nextInt();
System.out.println(numbHuman + " players selected.");
while (numbHuman < 2 || numbHuman > 8)
{
System.out.println("Invalid entry, try again.");
numbHuman = scan.nextInt();
}
arr = new Player[numbHuman];
userName = new String[numbHuman];
userID = new int[numbHuman];
startingMoney = 1500;
startingPosition = 0;
b=0;
for(int i = 0; i < arr.length; i++)
{

userID[i] = b;
System.out.println("Player " + (i + 1) + ", Please enter your first name:");
userName[i] = scan.next();
arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition);
b++;
}
}

public int viewNumbHuman()
{
return numbHuman;
}

public Player[] getPlayerArray()
{
return arr; //WORKS HERE...
}
}

这是无法编译的类...

public class Planet
{
Scanner scan = new Scanner(System.in);

private static Player[] arr;

private static Planet[] planetArray;

private int cost;

private int playerPosition;

public Planet()
{

Planet testPlanet = new Planet();
PlanetInfo[] planetArray;
planetArray = new PlanetInfo[40];

planetArray[1] = new PlanetInfo("Tatooine Mos Eisley",60,1,1,-1,2,0,0);
planetArray[3] = new PlanetInfo("Tatooine Mos Espa",60,3,1,0,-1,0,0);

//MORE of this, not important


planetArray[37] = new PlanetInfo("Coruscant Jedi Temple",350,37,8,-1,35,0,0);
planetArray[39] = new PlanetInfo("Coruscant Senate",400,39,8,-1,50,0,0);
}

public PlanetInfo[] getPlanetArray()
{
return planetArray;
}

public int testCost()
{
return cost;
}

public int returnCost()
{
return planetArray[arr[0].viewPosition()].testCost(); //**** HERE *WAS* THE ISSUE
}
/*
public int what()
{
return planetArray[arr[0].viewPosition()];
}
*/

最佳答案

字段 planetArray 声明为

private static Planet[] planetArray;

并且您尝试在带有签名的方法中返回它

public PlanetInfo[] getPlanetArray()

你的方法

public void Planets()

有一个您期望类型的局部变量,但这与 getPlanetArray 方法无关。

Planets 方法也是一个令人困惑的方法,因为看起来您可能打算创建一个构造函数,但给了它错误的签名作为构造函数并实例化了 Planets 其中的对象。

关于Java ~ 返回数组时类型不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305389/

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