0

我想让用户将字符串分配给 2 个数组。然后我希望他能够在分配后键入一个数字以提取分配给字符串的值。

import java.util.Scanner;
public class Main {

    public static void main (String[]args) {

    Scanner keyboard = new Scanner(System.in);

    String customerList[] = new String[2];

    customerList[1] = keyboard.nextLine();
    customerList[2] = keyboard.nextLine();


    System.out.println("Which array  value  would you like to see?");

    int listNum = keyboard.nextInt();

        System.out.println(customerList[listNum]);
    }

}
4

1 回答 1

4

数组的第一个值索引为 0 而不是 1:

customerList[0] = keyboard.nextLine();
customerList[1] = keyboard.nextLine();

这是一个很棒的 Java 数组教程:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

于 2013-07-07T17:53:36.580 回答