gpt4 book ai didi

java - 如何使用用户输入更改对象的名称?

转载 作者:行者123 更新时间:2023-12-02 05:57:54 25 4
gpt4 key购买 nike

我如何使用用户输入更改对象的名称。对于前。我要求用户以字符串形式输入他们的 id。我想用它来创建一个构造函数。

例如:

RefUnsortedList<Patients> NewList = new RefUnsortedList<Patients>();
Patients entry1=null;
System.out.println("Please enter patient's ID: ");
String TargetID = scan.next();

我要设置

Patients entry1 = null;

做到这一点

Patients "the user input here" = null;

最佳答案

java中没有动态变量,必须在源代码中声明。

您可以尝试使用 Map 并为变量的每个实例分配一个键。

Map patientMap = new HashMap();
patientMap.put("entry1", new Patients());
patientMap.put("the user input here", new Patients());

然后,当您想要检索患者时,您可以使用:

Patients patient = patientMap.get("the user input here");

关于java - 如何使用用户输入更改对象的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com