gpt4 book ai didi

Java - 将用户输入添加到 ArrayList,需要用另一个 ArrayList 中的变量分隔

转载 作者:行者123 更新时间:2023-12-01 10:21:11 25 4
gpt4 key购买 nike

我的模板打开时会显示一个选项菜单。当用户选择选项 1 时,它会要求他们输入一个数字 teamNumber。必须实例化类 Team,然后将其写入数组列表。

如果numberList中至少有一个数字,用户可以选择选项2。它要求他们输入数组列表中的任意数字并进行搜索。如果找到他们输入的号码,则您输入团队成员。它将输入写入位于类 TeamMember 中的私有(private)数组列表。

除了一个问题之外,我已经非常接近解决所有问题了。看来,无论我在选择选项 2 后输入哪个存储的团队编号,我添加到 teamList 的团队成员都会添加到所有团队编号中,而不仅仅是我想要添加到的团队编号中。因此,假设在选项 1 中我添加了团队 1 和团队 2。然后在选项 2 中,我键入 1 并继续输入成员。最终将该团队成员添加到团队 1 和团队 2。有没有办法解决这个问题?我尝试了几种选择,但似乎无法理解它。

public class Main {
public static void main(String[] args) {
int choosing;
Scanner scan = new Scanner(System.in);
String input;
int teamNumber;
boolean stayInLoop;
ArrayList<Team> numberList = new ArrayList<Team>();
do {
stayInLoop = true;
System.out.println("1. Add a new team");
System.out.println("2. Add a new team member");
System.out.println("3. View teams");
input = scan.nextLine();

if (input.equals("1")) {
System.out.println("Enter a team number:");
teamNumber = scan.nextInt();
scan.nextLine();
Team addTeam = new Team(teamNumber);
numberList.add(addTeam);
}

if (input.equals("2")){
boolean foundIt = false;
boolean valid = true;
System.out.println("Team number:");
teamNumber = scan.nextInt();
scan.nextLine();
for (int a = 0; a < numberList.size() && foundIt == false; a++){
Team addTeam = numberList.get(a);
if (addTeam.findTeam() == teamNumber) {
foundIt = true;
System.out.println("Enter first name of team member:");
String teamMemberFirstName = scan.nextLine();
System.out.println("Enter first initial of last name:");
char firstInitialLastName = scan.nextLine().charAt(0);
TeamMember inputTeamMember = new TeamMember(teamMemberFirstName, firstInitialLastName);
inputTeamMember.addMember(inputTeamMember, valid = true);
int teamSize = (inputTeamMember.size(valid = true));
System.out.println("Team " + teamNumber + " has " + teamSize + " members!");
}
}
if (foundIt == false) {
System.out.println("Try again.");
}
}
}while (stayInLoop == true;)
}}

团队成员:

public class TeamMember {

//the code provided in the task had teamList set to private, so I'm assuming it's required to be that way. I added static in order to eventually be able to call it with a method to view the team in option 3. But if the only solution involves changing the list to public, then so be it.
private final String teamMemberFirstName;
private final char firstInitialLastName;
private static ArrayList<TeamMember> teamList = new ArrayList<>();

public TeamMember(String teamMemberFirstName, char firstInitialLastName) {
this.teamMemberFirstName = teamMemberFirstName;
this.firstInitialLastName = firstInitialLastName;
}
public int addMember(TeamMember member, boolean valid) {
valid = teamList.add(member);
return teamList.size();
}
public static int size(boolean valid) {
return teamList.size();
}
}

最佳答案

teamList 是静态的,因此由 TeamMember 的所有实例共享。删除 static 关键字。

关于Java - 将用户输入添加到 ArrayList,需要用另一个 ArrayList 中的变量分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607399/

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