它工作正常,但循环同时显示索引0和1
并且它仅适用于int
,对于String
它不能按预期工作:
Scanner scan = new Scanner(System.in);
System.out.print("How many city that you ever visited: ");
int data = scan.nextInt();
String city[] = new String[data];
System.out.println("Input the name of city: ");
for (int i = 1;i <= data; i++) {
System.out.print("City " + i + " : ");
city[i] = scan.nextLine();
}