提问者:小点点

未打印字符串名称


import java.util.*;

//student class
class Student{
    String name;
    int rollNo;
    
    Student(String name, int rollNo){
        this.name=new String(name);
        this.rollNo=rollNo;
    }
    

}
class Demo {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        int noOfStudents = in.nextInt();
        Student[] StudentList= new Student[noOfStudents];
        PriorityQueue<Student> set=new PriorityQueue<Student>(new Comparator<Student>(){
            public int compare(Student a, Student b){
                return b.rollNo-a.rollNo;
            }
        });
        for(int i=0;i<noOfStudents;i++){
            String name = in.nextLine();
            in.nextLine();
            int rollNo = in.nextInt();
            set.add(new Student(name,rollNo));
        }
        
        while(!set.isEmpty()){
            Student tmp = set.poll();
            System.out.println(tmp.name+" "+tmp.rollNo);
        }   
        
    }
}

我正试着取n个学生的名字,卷不,然后打印出来。 但这不是打印学生姓名

我添加了额外的nextline()来启用整数项

我在这件事上总觉得吃力。 请救命!


共2个答案

匿名用户

替换

        String name = in.nextLine();
        in.nextLine();
        int rollNo = in.nextInt();

        in.nextLine();
        String name = in.nextLine();
        int rollNo = in.nextInt();

您可以在以下网址找到问题的完整解释:https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/

匿名用户

更改:

String name = in.nextLine();
in.nextLine();
int rollNo = in.nextInt();

String name = in.next();
int rollNo = in.nextInt();