public static String[][] possibleOutcomes(Scanner fileScan,int weaponNumber)
{
int numberOfOutcomes = (int)Math.pow(weaponNumber,2);
String[][] outcomes = new String[numberOfOutcomes][numberOfOutcomes];
String line = fileScan.nextLine();
Scanner lineScan = new Scanner(line);
fileScan.nextLine();
fileScan.nextLine();
while (fileScan.hasNextLine())
{
String userWeapon = lineScan.next();
String computerWeapon = lineScan.next();
String possibleTie = lineScan.next();
if (possibleTie.equals("ties"))
outcomes[userWeapon][computerWeapon] = possibleTie;
else
outcomes[userWeapon][computerWeapon] = lineScan.next();
}
return outcomes;
}
错误消息:我认为这是我的输入是整数,即使它们设置为String。我该怎么办?
RPSL.java:57:错误:类型不兼容:字符串无法转换为
整型
结果[userWeapon] [computerWeapon] =可能的领带;
RPSL.java:57:错误:类型不兼容:字符串无法转换为
整型
结果[userWeapon] [computerWeapon] =可能的领带;
最佳答案
您将userWeapon
和computerWeapon
声明为字符串,无法使用它们来访问数组。而是从扫描仪读取一个整数(请参见Scanner#nextInt
)。
int userWeapon = lineScan.nextInt();
int computerWeapon = lineScan.nextInt();
String possibleTie = lineScan.next();
if (possibleTie.equals("ties"))
outcomes[userWeapon][computerWeapon] = possibleTie;
else
outcomes[userWeapon][computerWeapon] = lineScan.next();