例5.7三个字符串求最大者
1 public class cjava { 2 public static void main(String[] args) { 3 System.out.println("输入三个国家的名字:"); 4 Scanner s = new Scanner(System.in); 5 String [] strings = new String[3]; 6 for(int i = 0;i<strings.length;i++){ 7 strings [i] = s.next(); 8 } 9 String temp = ""; 10 for(int i = 0;i<strings.length-1;i++){ 11 for(int j = 1;j<strings.length;j++) 12 if(strings[i].compareTo(strings[j])<0){ 13 temp = strings[i]; 14 strings[i] = strings[j]; 15 strings[j] = temp; 16 } 17 } 18 System.out.println(strings[0]); 19 } 20 }
例5.8 字符串从大到小排列
1 public class cjava { 2 public static void main(String[] args) { 3 System.out.println("输入三个字符串:"); 4 Scanner s = new Scanner(System.in); 5 String [] strings = new String[3]; 6 for(int i = 0;i<strings.length;i++){ 7 strings [i] = s.next(); 8 } 9 String temp=""; 10 if(strings[2].compareTo(strings[1])<0){ 11 temp=strings[1]; 12 strings[1]=strings[2]; 13 strings[2]=temp; 14 } 15 if(strings[0].compareTo(strings[1])<=0){ 16 System.out.print(strings[0]+" "+strings[1]+" "+strings[2]); 17 } 18 else if(strings[0].compareTo(strings[2])<=0){ 19 System.out.print(strings[1]+" "+strings[0]+" "+strings[2]); 20 } 21 else System.out.print(strings[1]+" "+strings[2]+" "+strings[0]); 22 } 23 24 }
2.遇到的问题:无
3.明天继续写例题