0

到目前为止,这是我的代码:

Scanner scanner = new Scanner(System.in); //Scanning all user input

// Dialogue
System.out.println("Welcome to jAddress!");
System.out.println("Please tell me your name and phone number?");
String personalInfo = scanner.nextLine();
System.out.println("So your name & number is " + personalInfo);

//-----------------------------------------------------------------------------
String[] theList; // setting the type and name of the Array
theList = new String[6];// giving the array account of how many

/*Inputting the Name & Number Of Each Individual and storing it*/
personalInfo = theList[0]; // This one stores your personalInfo into theLis[0]

System.out.println("Do you Have a Name & Number You'd Like to Input? y/n");
String yesNo = scanner.nextLine();
String y = "yes"; String n = "no";
if(yesNo == y){
    System.out.println("Yay:D");
}else if(yesNo == n){
    System.out.println("GoodBye");
    //break;
}

System.out.println("Please Enter Name & Phone Number:");
theList[1] = scanner.nextLine(); //Storing Number & Name for theList[1]

System.out.println("Please Enter Name & Phone Number:");
theList[2] = scanner.nextLine(); //Storing Number & Name for theList[2]

System.out.println("Please Enter Name & Phone Number:");
theList[3] = scanner.nextLine(); //Storing Number & Name for theList[3]

System.out.println("Please Enter Name & Phone Number:");
theList[4] = scanner.nextLine(); //Storing Number & Name for theList[4]

System.out.println("Please Enter Name & Phone Number:");
theList[5] = scanner.nextLine(); //Storing Number & Name for theList[5]

System.out.println("If you want to access any name simply Press 0-5.");
int chooseContact = scanner.nextInt();

我设法将两个数组链接到用户输入。因此,用户可以手动输入姓名和电话号码String,然后将其存储在其中一个数组中。

但是,我希望最后的用户能够搜索数组并让它打印姓名和电话号码。例如,我希望用户输入1然后输出数组theList[0]编号和名称。

我怎样才能做到这一点?我正在寻找一种简单的方法来实现这一点,例如分配变量等。

4

1 回答 1

0

您可以简单地使用用户输入的索引(+/- 1,取决于 0-5/1-6 编号的情况)theList[index]

于 2013-06-08T21:26:30.060 回答