从低到高显示标记

我正在尝试从用户输入中从最低到最高显示标记。代码获取学生姓名数组和标记数组。然后它必须显示从最低到最高分数的输出以及学生姓名。我很震惊在显示学生姓名时。代码仅显示从最低到最高的分数。


int n = 5;//total no of students

Scanner in = new Scanner(System.in);

System.out.println("Enter Students name:");

String[] s = new String[n];

for (int i = 0; i<s.length; i++)

{

  s[i] = in.nextLine();

}

System.out.println("Enter marks:");

int array[] = new int[n]; 

for (int i = 0; i<n; i++) 

{

 array[i] = in.nextInt();

}

Arrays.sort(array);

for (int i = 0; i < array.length; i++) 

{

  System.out.println(array[i]);

}


MMTTMM
浏览 107回答 2
2回答

慕标琳琳

创建一个包装类(比如Student)来存储学生姓名和分数。然后使用比较器对它们进行排序以比较标记。import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;public class testing {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int n = 5;//total no of students&nbsp; &nbsp; &nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Students name:");&nbsp; &nbsp; &nbsp; &nbsp; Student[] students = new Student[n];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i<n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[i] = new Student();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[i].name = in.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter marks:");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i<n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[i].mark = in.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(students, new MarkComparator());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(students[i].name + " - "+ students[i].mark);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class Student {&nbsp; &nbsp; int mark;&nbsp; &nbsp; String name;}class MarkComparator implements Comparator {&nbsp; &nbsp; public int compare(Object o1,Object o2){&nbsp; &nbsp; &nbsp; &nbsp; Student s1=(Student)o1;&nbsp; &nbsp; &nbsp; &nbsp; Student s2=(Student)o2;&nbsp; &nbsp; &nbsp; &nbsp; if(s1.mark>=s2.mark) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}示例输出:Enter Students name:alexberthachrisdanervinEnter marks:5040302010ervin - 10dan - 20chris - 30bertha - 40alex - 50

动漫人物

这应该适合你// Class studentpublic class Student implements Comparable {&nbsp; &nbsp; public String Name;&nbsp; &nbsp; public double Mark;&nbsp; &nbsp; // Should implement comparable to be able to compare it via Marks&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Student per) {&nbsp; &nbsp; &nbsp; &nbsp; if(this.Mark == per.Mark)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.Mark > per.Mark ? 1 : -1;&nbsp; &nbsp; }}//total no of studentsint n = 5;// list of students and their marksStudent[] s = new Student[n];// ScannerScanner in = new Scanner(System.in);// Get student names and their marksfor (int i = 0; i<s.length; i++){&nbsp; &nbsp; s[i] = new Student();&nbsp; &nbsp; System.out.println("Enter Student #" + i + " name :");&nbsp; &nbsp; s[i].Name = in.nextLine();&nbsp; &nbsp; System.out.println("Enter Student #" + i + " mark :");&nbsp; &nbsp; s[i].mark = in.nextDouble();}Arrays.sort(s);for (int i = 0; i < s.length; i++)&nbsp;{&nbsp; &nbsp; System.out.println(s[i].Name + " : " + s[i].Mark);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java