我已使用输入获取值并将其添加到ArrayList。当我第二次输入数据时,它会在输入第二个数据后覆盖第一个数据。无法修复它。
学生班
public class Student {
static int rollNumber, age;
static String firstName, lastName, gender;
public Student(int rollNumber, String firstName, String lastName, int age, String gender)
{
this.rollNumber = rollNumber;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
}
InputParticipant类
import java.util.*;
public class InputParticipant extends Student{
public static String sportsEvent;
public InputParticipant(int rollNumb, String firstName, String lastName,
int age, String gender, String sportsEvent)
{
super(rollNumber, firstName, lastName, age, gender);
this.sportsEvent=sportsEvent;
}
public void elegibility()
{
System.out.print("Eligibility:");
//50 meter race
if(age>=10 && age<=12 && gender.equals("female") && sportsEvent.equals("50meterrace"))
{
System.out.println(" Yes ");
}
else if((age>=10 && age<=15) && sportsEvent.equals("100meterrace"))//100 meter race
{
System.out.println(" yes ");
}
else if(age>=15 && (sportsEvent.equals("400meterrace") || sportsEvent.equals("Juvelin") || sportsEvent.equals("discus")))//400 meter race, Juvelin and discus
{
System.out.println(" Yes ");
}
else
{
System.out.println(" No (age:Min 10-12 for 50m race-female only, Min 10-15 for 100m race and Min 15 for 400m race, Juvelin and discus)");
}
}
public static void main(String[] args)
{
List<InputParticipant> stud=new ArrayList<InputParticipant>();
InputParticipant i;
i=new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println("Enter the Student details:");
System.out.print("roll number:");
rollNumber=in.nextInt();
System.out.print("First name:");
firstName=in.next();
System.out.print("Last name:");
lastName=in.next();
System.out.print("age:");
age=in.nextInt();
System.out.print("gender:");
gender=in.next();
System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):");
sportsEvent=in.next();
stud.add(i);
List<InputParticipant> l=new ArrayList<InputParticipant>();
l.addAll(stud);
Iterator<InputParticipant> disp= l.iterator();
while(disp.hasNext())
{
InputParticipant st=disp.next();
System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent);
st.elegibility();
System.out.println("-----------------------------------------");
}
}
}
}
答案 0 :(得分:2)
因为你在循环中创建了新对象: List l = new ArrayList();
尝试这样的事情:
public static void main(String[] args) {
List<InputParticipant> stud=new ArrayList<InputParticipant>();
//List<InputParticipant> l=new ArrayList<InputParticipant>();
Scanner in = new Scanner(System.in);
while(true){
System.out.println("Enter the Student details:");
System.out.print("roll number:");
rollNumber=in.nextInt();
System.out.print("First name:");
firstName=in.next();
System.out.print("Last name:");
lastName=in.next();
System.out.print("age:");
age=in.nextInt();
System.out.print("gender:");
gender=in.next();
System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):");
sportsEvent=in.next();
stud.add(i);
InputParticipant i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);
Iterator<InputParticipant> disp = l.iterator();
while(disp.hasNext()){
InputParticipant st=disp.next();
System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent);
st.elegibility();
System.out.println("-----------------------------------------");
}
}
}
答案 1 :(得分:0)
可能尝试使用Java POJO。
在您的情况下,从Scanner获取值后,创建一个新Object,然后将该对象添加到ArrayList。
i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);
stud.add(i);
不确定为什么要在while循环中创建另一个ArrayList,因为while应仅用于收集输入。
答案 2 :(得分:0)
我无法真正帮助您修复代码,因为很多事情都是很奇怪。
为什么看起来你总是重写可能与创建Student static 的所有类成员有关,这意味着Student类的每个实例都具有相同的名称,firstname,...
- 也可以从创建一个Student类开始,该类只包含带有getter和setter的私有成员以及所有这些属性的构造函数。
- 然后在另一个Main类中创建main方法,这将阻止你做你不应该做的事情。
- 在该方法实例化一个List,此后开始一个循环。
- 在该循环中,您将1名学生的数据收集到局部变量中(String name = in.readNext(); String firstName = ....)
- 您使用构造函数实例化Student对象,只需将本地变量传递给它。
- 将新创建的Student对象添加到列表中。
- 循环完成后,您应该有一个填充学生的列表。
答案 3 :(得分:0)
Student
类和InputParticipant
类的成员都是静态的。阅读static members。
换句话说,ArrayList
没有被覆盖,静态成员就像是同一个类的不同对象之间的共享变量。如果将某个内容分配给一个对象中的静态成员,则该成员将在所有对象中具有新值。只需删除static关键字并使其成为实例成员。
public class Student {
public int rollNumber, age;
public String firstName, lastName, gender;
public Student(int rollNumber, String firstName, String lastName, int age, String gender)
{
this.rollNumber = rollNumber;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
}