-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practice40.java
57 lines (53 loc) · 1.32 KB
/
Practice40.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package fiftypratice;
import java.util.Scanner;
/**
* 题目:字符串排序。
*
* */
public class Practice40 {
public static void main(String[] args) {
System.out.println("请输入字符串数组字符串数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println("请输入一个字符串数组:");
String [] arr=new String[n+1];
for(int i=0;i<arr.length;i++){
arr[i]=s.nextLine();
}
if(s!=null){
s.close();
}
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(compare(arr[i],arr[j])==false){
String t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}
}
System.out.println("排序后:");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
public static boolean compare(String s1,String s2){
boolean flag=true;
for(int i=0;i<s1.length()&&i<s2.length();i++){
if(s1.charAt(i)>s2.charAt(i)){
flag=false;
break;
}else if(s1.charAt(i)<s2.charAt(i)){
flag=true;
break;
}else {
if(s1.length() < s2.length()) {
flag = true;
} else {
flag = false;
}
}
}
return flag;
}
}