gpt4 book ai didi

java - 如何使用扫描仪在集合中动态添加数据

转载 作者:行者123 更新时间:2023-12-02 11:06:11 24 4
gpt4 key购买 nike

帮助我扫描用户的姓名和年龄,而不是在 ArrayList 中预定义。

我在以下程序中初始化了姓名和年龄,帮助我扫描用户的姓名和年龄,它应该进行比较,然后应该使用集合进行排序。

public class Control {
public static void main(String[] args) {

List<Student> studs = new ArrayList<Student>();
Student stu1 = new Student(22, "Gokul");
Student stu2 = new Student(12, "Dolby");
Student stu3 = new Student(24, "Rahul");
Student stu4 = new Student(56, "Raj");

studs.add(stu1);
studs.add(stu2);
studs.add(stu3);
studs.add(stu4);

Collections.sort(studs,new StudAge());
for(Student stud : studs) {
System.out.println(stud);
}


}

}

最佳答案

给你

public class Control {

private static Scanner sc; //Making sc as a field variable so that we no longer need to explicitly close the resource

public static void main(String[] args) {
System.out.println("Please enter your data ");
sc = new Scanner(System.in);
List<Student> studs = new ArrayList<Student>();
Student stu1 = new Student(sc.nextInt(), sc.nextLine()); //Dynamically asking user to input age and name

System.out.println("Please enter the Age and Name of the first user : ");
Student stu2 = new Student(sc.nextInt(), sc.nextLine());

System.out.println("Please enter the Age and Name of the second user : ");
Student stu3 = new Student(sc.nextInt(), sc.nextLine());

System.out.println("Please enter the Age and Name of the third user : ");
Student stu4 = new Student(sc.nextInt(), sc.nextLine());

studs.add(stu1);
studs.add(stu2);
studs.add(stu3);
studs.add(stu4);

Collections.sort(studs, new StudAge());
for (Student stud : studs) {
System.out.println(stud);
}

}

There are still ways to optimize this, but if using scanner to take Dynamic Input from user is your only goal, then, this will do the trick.

这是为您量身定制的代码。

public class Control {

private static Scanner sc; //Making sc as a field variable so that we no longer need to explicitly close the resource

public static void main(String[] args) {
System.out.println("Please enter your data \n\n\n");
sc = new Scanner(System.in);
List<Student> studs = new ArrayList<Student>();

// Asking the user to enter number of records he/she wishes to enter
System.out.println("How many records would you like to enter : ");

for (int i = sc.nextInt(); i > 0; i--) {

System.out.println("Please enter the Name : "); //Asking user for the name
String name = sc.next();

System.out.println("Please enter the age of " + name + " :"); //Asking user for the age of that specific name
int age = sc.nextInt();

studs.add(new Student(age, name));
}

Collections.sort(studs, new StudAge());
for (Student stud : studs) {
System.out.println(stud);
}

}

}

This last code is memory efficient as it saves you the space of creating multiple number of reference variables and then add them to the List. Instead we directly create and pass the object inside the add method of the List along with its parameters.

关于java - 如何使用扫描仪在集合中动态添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941160/

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