我正在处理一个项目并且我不断收到错误消息说错误:在尝试编译时,不兼容的类型int []无法转换为int。错误说它在第17行。我已经尝试将所有内容更改为双打,并且我为双打获得了相同的错误。我不知道为什么数字无法存储,解释会非常有用。
int[][] houseNumbers;
int numberOfHouses, homeNumber, numberOfPeople, house, people;
System.out.print("How many houses in the street?: ");
numberOfHouses = keyboard.nextInt();
houseNumbers = new int[numberOfHouses][];
for (house = 0; house < houseNumbers.length; house++){
System.out.print("What is the next house number? : ");
houseNumbers[house] = keyboard.nextInt();
System.out.print("How many people live in house number" + houseNumbers[house] + ":");
numberOfPeople = keyboard.nextInt();
houseNumbers[house] = new int[numberOfPeople];
for (people = 0; people < houseNumbers[house].length; people++){
System.out.print("What is the age of person " + people + ": ");
houseNumbers[house][people] = keyboard.nextInt();
}
}
答案 0 :(得分:0)
你有几个错误。最大的一个是尝试将int
分配给int
数组。数组是(一个或多个)项的块。您必须先使用new
分配块,然后才能访问各个项目。你的编译器告诉你这个。学习理解编译器的错误和警告。
错误1:
/* houseNumbers[house] -> */ homeNumber = keyboard.nextInt();
错误2:
System.out.print("How many people live in house number" + /* houseNumbers[house] -> */ homeNumber + ":");