如何编写一个方法来询问在Array中更新用户信息的索引和值


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class UpdateUserInfo { 
public static void main(String[] args) {
ArrayList<String> userList = new ArrayList<>(Arrays.asList("Name", "LastName", "08/19/1995",
"123 main Street, mcLean, VA 22102", "7576330954"));
// To update user's information, please edit value in double quotes, this is a basic way, but can we create our own method for it
//        userList.set(0, "Name"); // name
//        userList.set(1, "LastName");
//        userList.set(2, "08/19/1995");
//        userList.set(3, "123 main Street, mcLean, VA 22102");
//        userList.set(4, "7576330954");
System.out.print(userList);
// changeUserInfo();
}

//这是我尝试创建自己的方法

public static void changeUserInfo(String[] name, String lName, String dob, String address, String phone){
Scanner scan = new Scanner(System.in);
System.out.println("Do you wish to update your info? ");
boolean flag = scan.hasNextBoolean();
if(flag == true){
System.out.println("What info do you want to update? Name? LastName?");
}
}
}

我想添加布尔值,看看他们的用户是否想更改信息,如果是,然后更改它。

import java.util.Arrays;
import java.util.Scanner;
public class UpdateUserInfo {
public static void main(String[] args) {
String[] arrayList = { "Name", "LastName", "08/19/1995",
"123 main Street, mcLean, VA 22102", "7576330954"};
Scanner scan = new Scanner(System.in);
System.out.println("to edit the name type: 0, nto edit the last name type: 1nto edit birthday type:2n" +
"to edit address type:3nand to edit id type:4");
int option = scan.nextInt();
System.out.println("change the value: ");
String desire = scan.next();// this is where you input the replacement value
scan.close();
System.out.println(" before:");
System.out.println(Arrays.deepToString(arrayList));
System.out.println(" after:");
System.out.println(Arrays.deepToString(changeUserInfo(arrayList,option,desire)));
}
public static String[] changeUserInfo(String[] array,int value, String to){
array[value] = to;// the [value] is to access the list in the desired area you want to make  a change.
return array;
}
}

在这里,我创建了一个菜单,让用户根据他想要更改的值来选择数字。之后,我创建了一个返回数组的方法,并使用";值";作为索引,并进行了更改。

我相信您正在尝试为人员列表创建一个输入表单。您可能想要引入一个User类,该类将具有用户的所有字段。

关于更新用户流,标志布尔检查内部的部分应该使用某种标识符和值机制。在一个条目中选择的标识符将具有作为下一个输入传递的值。

作为第一句话,你应该询问用户的id或电子邮件或任何其他唯一标识符,信息必须更新。

接下来,显示字段列表和用户将输入的相应数字。人们更喜欢使用参考资料而不是键入长单词,拼写错误的麻烦很麻烦。枚举是管理具有相应数字的字段名的好方法。

用户输入数字(标识符(后,显示将更新哪个字段的提示,并在括号中显示示例值。然后,用户可以输入新值,您也可以更新信息。

您可以在不询问id的情况下提示next为同一用户更新不同的字段,也可以重新启动更新流程。干杯

最新更新