英文:
Objects are created without intention
问题
我正在尝试创建一个LinkedList,然后创建一个菜单,询问是否要创建一个教师,显示教师列表。
问题是,我可以创建教师对象并将其添加到LinkedList中,但是,当我选择第二个选项,多次显示教师列表而不创建新的教师时,程序继续创建对象。
以下是我的代码:
```java
import java.util.*;
import javax.swing.JOptionPane;
public class Prueba {
public static void main(String[] args) {
LinkedList<Teacher> teacher = new LinkedList();
int id, salary, op;
String name, program, teacherList = "";
Teacher t1 = new Teacher();
op = Integer.parseInt(JOptionPane.showInputDialog("请选择一个选项:\n" +
"1. 添加教师。\n" +
"2. 获取教师列表。\n"));
while (op != 0) {
if (op == 1) {
id = Integer.parseInt(JOptionPane.showInputDialog("请输入教师ID:"));
name = JOptionPane.showInputDialog("请输入教师姓名:");
program = JOptionPane.showInputDialog("请输入教师所在项目:");
salary = Integer.parseInt(JOptionPane.showInputDialog("请输入教师工资:"));
Teacher d = new Teacher(id, name, program, salary);
teacher.add(d);
} else if (op == 2) {
Iterator p = teacher.iterator();
while (p.hasNext()) {
teacherList = teacherList + "\n" + p.next();
}
JOptionPane.showMessageDialog(null, teacherList);
}
op = Integer.parseInt(JOptionPane.showInputDialog("请选择一个选项:\n" +
"1. 添加教师。\n" +
"2. 获取教师列表。\n"));
}
}
}
教师类(Teacher Class):
public class Teacher {
private int id;
private String name;
private String program;
private int salary;
public Teacher() {
}
public Teacher(int id, String name, String program, int salary) {
this.id = id;
this.name = name;
this.program = program;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
String message;
message = "ID:" + this.id + " - 姓名:" + this.name + " - 项目:" + this.program + " - 工资:$" + this.salary;
return message;
}
}
希望这可以帮助你解决问题。
英文:
I am trying to create a LinkedList and then create a menu asking to create a Teacher, Show Teacher list.
The thing is that I can create the Teacher object and add it to the LinkedList, but, when I select the Second option, Show Teacher List more than one time without creating a new Teacher, the programs continue to creating objects.
Here is my code:
import java.util.*;
import javax.swing.JOptionPane;
public class Prueba {
public static void main(String[] args) {
LinkedList<Teacher> teacher = new LinkedList();
int id, salary,op;
String name, programm, teacherList ="";
Teacher t1 = new Teacher();
op=Integer.parseInt(JOptionPane.showInputDialog("Please, pick an option: \n"
+ "1. Add Teacher. \n"
+ "2. Get Teacher List. \n"
));
while(op!=0){
if(op == 1){
id=Integer.parseInt(JOptionPane.showInputDialog("Ingrese ID del docente: "));
name=JOptionPane.showInputDialog("Ingrese nombre del docente: ");
programm=JOptionPane.showInputDialog("Ingrese programa del docente: ");
salary=Integer.parseInt(JOptionPane.showInputDialog("Ingrese salario del docente: "));
Teacher d = new Teacher(id,name,programm,salary);
teacher.add(d);
}else if(op == 2){
Iterator p = teacher.iterator();
while(p.hasNext()){
teacherList = teacherList + "\n"+ p.next();
}
JOptionPane.showMessageDialog(null, teacherList);
}
op=Integer.parseInt(JOptionPane.showInputDialog("Please, pick an option: \n"
+ "1. Add Teacher. \n"
+ "2. Get Teacher List. \n"
));
}
}
}
Class Teacher:
public class Teacher {
private int id;
private String name;
private String programm;
private int salary;
public Teacher() {
}
public Teacher(int id, String name, String programm, int salary) {
this.id = id;
this.name= name;
this.programm = programm;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
public String getProgramm() {
return programm;
}
public void setProgramm(String programm) {
this.programm = programm;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString(){
String message;
message = "Id: "+this.id+" - Name: "+this.name+" - Programm: "+this.programm+" - Salary: $"+this.salary;
return message;
}
}
答案1
得分: 1
在遍历列表时,您需要将每位教师的字符串表示(toString()
)添加到一个字符串中,并在遍历整个列表后显示该字符串。
请按以下方式执行:
public class Prueba {
public static void main(String[] args) {
LinkedList<Teacher> teachers = new LinkedList();
int id, salary, op;
String name, programm, teacherList = "";
Teacher teacher;
op = Integer.parseInt(JOptionPane
.showInputDialog("Please, pick an option: \n" + "1. Add Teacher. \n" + "2. Get Teacher List. \n"));
while (op != 0) {
if (op == 1) {
id = Integer.parseInt(JOptionPane.showInputDialog("Ingrese ID del docente: "));
name = JOptionPane.showInputDialog("Ingrese nombre del docente: ");
programm = JOptionPane.showInputDialog("Ingrese programa del docente: ");
salary = Integer.parseInt(JOptionPane.showInputDialog("Ingrese salario del docente: "));
teacher = new Teacher(id, name, programm, salary);
teachers.add(teacher);
} else if (op == 2) {
StringBuilder sb = new StringBuilder();
for (Teacher t : teachers) {
sb.append(t.toString()).append(System.lineSeparator());
}
JOptionPane.showMessageDialog(null, sb.toString());
}
op = Integer.parseInt(JOptionPane
.showInputDialog("Please, pick an option: \n" + "1. Add Teacher. \n" + "2. Get Teacher List. \n"));
}
}
}
注意:
- 我使用了一个
StringBuilder
变量来收集每位教师的字符串表示,以避免创建过多的字符串。如果您希望使用String
变量,也可以这样做。 - 我使用了增强的
for
循环来遍历列表(teachers
)。如果您希望使用Iterator
,也可以这样做。
英文:
While navigating the list, you need to add the string representation of each teacher (toString()
) to a string and display the string once you have iterated the whole list.
Do it as follows:
public class Prueba {
public static void main(String[] args) {
LinkedList<Teacher> teachers = new LinkedList();
int id, salary, op;
String name, programm, teacherList = "";
Teacher teacher;
op = Integer.parseInt(JOptionPane
.showInputDialog("Please, pick an option: \n" + "1. Add Teacher. \n" + "2. Get Teacher List. \n"));
while (op != 0) {
if (op == 1) {
id = Integer.parseInt(JOptionPane.showInputDialog("Ingrese ID del docente: "));
name = JOptionPane.showInputDialog("Ingrese nombre del docente: ");
programm = JOptionPane.showInputDialog("Ingrese programa del docente: ");
salary = Integer.parseInt(JOptionPane.showInputDialog("Ingrese salario del docente: "));
teacher = new Teacher(id, name, programm, salary);
teachers.add(teacher);
} else if (op == 2) {
StringBuilder sb = new StringBuilder();
for (Teacher t : teachers) {
sb.append(t.toString()).append(System.lineSeparator());
}
JOptionPane.showMessageDialog(null, sb.toString());
}
op = Integer.parseInt(JOptionPane
.showInputDialog("Please, pick an option: \n" + "1. Add Teacher. \n" + "2. Get Teacher List. \n"));
}
}
}
Notes:
- I've used a
StringBuilder
variable instead of aString
variable to collect the string representation of each teacher to avoid too many strings getting created. If you wish to use aString
variable for it, you can do so. - I've used enhanced
for
loop to navigate the list (teachers
). If you wish to useIterator
for it, you can do so.
答案2
得分: 0
尝试在 JOptionPane.showMessageDialog(null, teacherList);
后面添加 teacherList = ""
。
英文:
try add teacherList = ""
after JOptionPane.showMessageDialog(null, teacherList);
答案3
得分: 0
问题不在于对象被复制了,而在于您在没有清除teacherList变量的情况下将新文本添加到其中。
因此在以下代码片段之后(或之前):
while(p.hasNext()){
teacherList = teacherList + "\n" + p.next();
}
JOptionPane.showMessageDialog(null, teacherList);
您可能应该添加类似如下的内容:
teacherList = "";
英文:
The problem isn't that the objects are duplicated, it's that you are adding the new text to the teacherList variable without clearing it.
So after (or before)
while(p.hasNext()){
teacherList = teacherList + "\n"+ p.next();
}
JOptionPane.showMessageDialog(null, teacherList);
You should probably add something like
teacherList = "";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论