代码有什么问题?它不会让我为每一个编写输入。我想阅读每个版本的版本数量,年份和城市,它会跳转到循环中,不会让我输入。
public class Competition {
private static ArrayList<Edition> editions = new ArrayList<Edition>();
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String newCity = null;
int newYear = 0;
int itineration = 0;
int numberEditions = 0;
Edition newEdition = new Edition(newCity, newYear);
System.out.println("How many editions would you like to create?");
numberEditions = scan.nextInt();
while (itineration < numberEditions) {
System.out.println("\nEnter the city and year of the Edition you would"
+ " like to create: ");
System.out.println("Year: ");
newYear = scan.nextInt();
System.out.println("City: ");
newCity = scan.nextLine();
editions.add(newEdition);
itineration++;
}
for (int index = 0; index < editions.size(); index++) {
System.out.println("Music Festival'" + editions.get(index).getYear()
+ ", "
+ editions.get(index).getCity());
}
}
}
答案 0 :(得分:1)
您需要在阅读年份后消耗额外的费用。您还需要为每个输入创建新版本并将其添加到while循环列表中:
while (itineration < numberEditions) {
System.out.println("\nEnter the city and year of the Edition you would"
+ " like to create: ");
System.out.println("Year: ");
newYear = scan.nextInt();
scan.nextLine();
System.out.println("City: ");
newCity = scan.nextLine();
Edition newEdition = new Edition(newCity, newYear);
editions.add(newEdition);
itineration++;
}
希望有所帮助
答案 1 :(得分:0)
您需要在while循环中创建Edition
的新实例。
while (itineration < numberEditions) {
System.out.println("\nEnter the city and year of the Edition you would"
+ " like to create: ");
System.out.println("Year: ");
newYear = scan.nextInt();
System.out.println("City: ");
newCity = scan.nextLine();
Edition newEdition = new Edition(newCity, newYear);
editions.add(newEdition);
itineration++;
}
答案 2 :(得分:0)
如果您不知道如何使用Scanner类,则会出现一些问题。 Some people suggest using a buffered reade河我建议在使用任何一个之前研究这两个对象。
答案 3 :(得分:0)
你犯了2个错误。
Scanner.next()
获取输入String
。 Scanner.nextLine()
无效。工作代码:
while (itineration < numberEditions)
{
System.out.println("\nEnter the city and year of the Edition you would" + " like to create: ");
System.out.println("Year: ");
newYear = scan.nextInt();
System.out.println("City: ");
newCity = scan.next();
editions.add(new Edition(newCity, newYear));
itineration++;
}
答案 4 :(得分:0)
为Scanner创建新的对象。现在你可以输入值。
newCity = new Scanner(System.in).nextLine();