java实现对三个城市排序,答案如下
package nameyu;import java.util.Scanner;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.print("Enter the first city"); String city1=input.nextLine(); System.out.print("Enter the second city"); String city2=input.nextLine(); System.out.print("Enter the third city"); String city3=input.nextLine(); if(city1.compareToIgnoreCase(city2)<0){ if(city2.compareToIgnoreCase(city3)<0){ System.out.println("123"+city1+" "+city2+" "+city3); }else if(city1.compareToIgnoreCase(city3)>0){ System.out.println("312"+city3+" "+city1+" "+city2); } if(city3.compareToIgnoreCase(city2)<0){ if(city2.compareToIgnoreCase(city1)<0){ System.out.println("321"+city3+" "+city2+" "+city1); }else if(city3.compareToIgnoreCase(city1)>0){ System.out.println("132"+city1+" "+city3+" "+city2); } else if(city3.compareToIgnoreCase(city2)>0){ if(city3.compareToIgnoreCase(city1)<0){ System.out.println("231"+city2+" "+city3+" "+city1); } } if(city1.compareToIgnoreCase(city3)<0){ if(city1.compareToIgnoreCase(city2)>0) System.out.println("213"+city2+" "+city1+" "+city3); } } } }}
页:
[1]