对不起,如果我犯了一些愚蠢的错误。我正在完成一项学校任务,我应该创建一个问题跟踪计划。每个问题必须由对象表示,并且对象必须存储在ArrayList中。必须有一个菜单,用户可以选择创建新问题,将已创建的问题标记为已解决,查看已解决和未解决的问题。每当我输入一个新对象时,我所制作的ArrayList都没有正确存储对象。所有其他对象都被改为我输入的那个对象并且我坚持这个...任何帮助将不胜感激!谢谢!
这是我的代码(我认为问题始于“if(option == 1)”):
public static void main(String[] args) {
int option = 0;
Scanner input = new Scanner(System.in);
ArrayList<IssueTracking> myIssueList = new ArrayList<>();
IssueTracking issueNum = new IssueTracking();
mainMenu();
option = input.nextInt();
while (option != 1 && option != 2 && option != 3 && option != 4 && option != 0){
System.out.println("Invalid option, please choose 1, 2, 3, 4 or 0 in the menu.");
mainMenu();
option = input.nextInt();
}
while(option !=0){
while (option != 1 && option != 2 && option != 3 && option != 4 && option != 0){
System.out.println("Invalid option, please choose 1, 2, 3, 4 or 0 in the menu.");
mainMenu();
option = input.nextInt();
}
if (option == 1){
System.out.println("Please describe the issue bellow: ");
String issueDescription = input.nextLine();
issueDescription = input.nextLine();
issueNum.setIssueDescription(issueDescription);
issueNum.setIsSolved(Boolean.FALSE );
myIssueList.add(issueNum);
System.out.println(myIssueList.toString().replace("[", "").replace("]", "").replace(",", ""));
returnMenu();
mainMenu();
option = input.nextInt();
}
if (option == 2){
System.out.println("Please press 1 to mark the issue as solved: ");
Boolean isSolved = input.nextBoolean();
issueNum.setIsSolved(isSolved);
myIssueList.add(issueNum);
//System.out.println("\n"+myIssueList.toString().replace("[", "").replace("]", ""));
returnMenu();
mainMenu();
option = input.nextInt();
}
if (option == 3){
//View unsolved issues.
System.out.println(myIssueList);
returnMenu();
mainMenu();
option = input.nextInt();
}
if (option == 4){
//View solved issues.
System.out.println(issueNum.getIssueDeString()+issueNum.getIsSolved());
returnMenu();
mainMenu();
option = input.nextInt();
}
}
}
}
问题类:
public class IssueTracking {
private String issueDescription;
private Boolean issueSolved;
public void setIssueDescription(String issueDescription){
this.issueDescription = issueDescription;
}
public String getIssueDeString(){
return issueDescription;
}
public void setIsSolved (Boolean issueSolved){
this.issueSolved = issueSolved;
}
public Boolean getIsSolved() {
return issueSolved;
}
public String toString(){
return "The issue is: " + getIssueDeString() +"\n"+ "The issue is solved: " + getIsSolved()+"\n"; //
}
答案 0 :(得分:1)
基本上,您正在尝试一次又一次地重复使用同一个对象,因此您会看到一个对象中的更改字段也会影响另一个对象。你应该做的是:
if (option == 1) {
IssueTracking issueNum = new IssueTracking();
...//get or set values
myIssueList.add(issueNum);
}
同样地,你应该要求已经解决的问题编号,并且应该只更新那个问题(我会使用Map来解决这个问题,其中密钥将是问题编号,值将是IssueTracking)
答案 1 :(得分:0)
问题是您正在重复使用issueNum
对象 - 当您输入第一个条目时,它会添加到列表中,当您输入第二个条目时,您编辑现有条目,然后添加另一个副本相同的对象,等等。
创建新实例将解决此问题:
if (option == 1){
System.out.println("Please describe the issue bellow: ");
String issueDescription = input.nextLine();
issueDescription = input.nextLine();
issueNum=new IssueTracking();
issueNum.setIssueDescription(issueDescription);
issueNum.setIsSolved(Boolean.FALSE );
myIssueList.add(issueNum);
System.out.println(myIssueList.toString().replace("[", "").replace("]", "").replace(",", ""));
returnMenu();
mainMenu();
option = input.nextInt();
}