通过User Scanner使用数组输入创建一些String值

时间:2017-07-07 00:55:30

标签: java arrays string loops int

它工作正常,但循环同时显示索引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();
}

0 个答案:

没有答案