java簡單字串排序
阿新 • • 發佈:2019-01-28
Bill,54 Black,46 Bush,78 Frank,88 Jean,65 Jenny,77 Mark,96 Smith,98 White,85 Wood,83 import java.util.Scanner; class Person { String name; int score; } public class Main { public static void main(String[] args) { Scanner reader=new Scanner(System.in); int i; Person person[]=new Person[10]; for(i=0;i<10;i++) { person[i]=new Person(); person[i].name=reader.next(); } for(i=0;i<10;i++) { person[i].score=reader.nextInt(); } int j; for(i=0;i<10;i++) { for(j=0;j<9;j++) { int d=person[j].name.compareTo(person[j+1].name); if(d>0) { Person p=new Person(); p=person[j]; person[j]=person[j+1]; person[j+1]=p; } } } for(i=0;i<10;i++) { System.out.println(person[i].name+","+person[i].score); } } }