对象是无意创建的

huangapple go评论78阅读模式
英文:

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&lt;Teacher&gt; teacher = new LinkedList();
int id, salary,op;
String name, programm, teacherList =&quot;&quot;;
Teacher t1 = new Teacher();
op=Integer.parseInt(JOptionPane.showInputDialog(&quot;Please, pick an option: \n&quot;
+ &quot;1. Add Teacher. \n&quot;
+ &quot;2. Get Teacher List. \n&quot;
));
while(op!=0){
if(op == 1){
id=Integer.parseInt(JOptionPane.showInputDialog(&quot;Ingrese ID del docente: &quot;));
name=JOptionPane.showInputDialog(&quot;Ingrese nombre del docente: &quot;);
programm=JOptionPane.showInputDialog(&quot;Ingrese programa del docente: &quot;);
salary=Integer.parseInt(JOptionPane.showInputDialog(&quot;Ingrese salario del docente: &quot;));
Teacher d = new Teacher(id,name,programm,salary);
teacher.add(d);
}else if(op == 2){
Iterator p = teacher.iterator();
while(p.hasNext()){
teacherList = teacherList + &quot;\n&quot;+ p.next();
}
JOptionPane.showMessageDialog(null, teacherList);
}
op=Integer.parseInt(JOptionPane.showInputDialog(&quot;Please, pick an option: \n&quot;
+ &quot;1. Add Teacher. \n&quot;
+ &quot;2. Get Teacher List. \n&quot;
));
}
}

}

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 = &quot;Id: &quot;+this.id+&quot; - Name: &quot;+this.name+&quot; - Programm: &quot;+this.programm+&quot; - Salary: $&quot;+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"));
        }
    }
}

注意:

  1. 我使用了一个StringBuilder变量来收集每位教师的字符串表示,以避免创建过多的字符串。如果您希望使用String变量,也可以这样做。
  2. 我使用了增强的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&lt;Teacher&gt; teachers = new LinkedList();
int id, salary, op;
String name, programm, teacherList = &quot;&quot;;
Teacher teacher;
op = Integer.parseInt(JOptionPane
.showInputDialog(&quot;Please, pick an option: \n&quot; + &quot;1. Add Teacher. \n&quot; + &quot;2. Get Teacher List. \n&quot;));
while (op != 0) {
if (op == 1) {
id = Integer.parseInt(JOptionPane.showInputDialog(&quot;Ingrese ID del docente: &quot;));
name = JOptionPane.showInputDialog(&quot;Ingrese nombre del docente: &quot;);
programm = JOptionPane.showInputDialog(&quot;Ingrese programa del docente: &quot;);
salary = Integer.parseInt(JOptionPane.showInputDialog(&quot;Ingrese salario del docente: &quot;));
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(&quot;Please, pick an option: \n&quot; + &quot;1. Add Teacher. \n&quot; + &quot;2. Get Teacher List. \n&quot;));
}
}
}

Notes:

  1. I've used a StringBuilder variable instead of a String variable to collect the string representation of each teacher to avoid too many strings getting created. If you wish to use a String variable for it, you can do so.
  2. I've used enhanced for loop to navigate the list (teachers). If you wish to use Iterator for it, you can do so.

A sample output:
对象是无意创建的

答案2

得分: 0

尝试在 JOptionPane.showMessageDialog(null, teacherList); 后面添加 teacherList = &quot;&quot;

英文:

try add teacherList = &quot;&quot; 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 + &quot;\n&quot;+ p.next();
}
JOptionPane.showMessageDialog(null, teacherList);

You should probably add something like

teacherList = &quot;&quot;;

huangapple
  • 本文由 发表于 2020年5月4日 07:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定