Java 使用 if else 语句和 compareTo 按字母顺序排列三个城市
Java alphabetizing three cities using if else statements and compareTo
嗨,我非常不知道你会如何比较三件事。教科书例子是:
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.compareTo(city2) < 0)
System.out.println("The cities in alphabetical order are:");
System.out.println(city1);
System.out.println(city2);
else
System.out.println("The cities in alphabetical order are:");
System.out.println(city2);
System.out.println(city1);
那么你如何比较第三个并按字母顺序排列?
您可以使用内置的排序算法(或您自己的)对 字母顺序 进行排序,术语是 词典顺序 。例如,通过使用 Collections#sort
(documentation)。请注意,String
是可比较的,默认情况下使用 字典顺序 。这就是为什么您不需要显式指定顺序的原因,例如使用 Comparator
对象。
此代码段对城市进行排序并打印出来:
List<String> cities = Arrays.asList({city1, city2, city3});
Collections.sort(cities);
System.out.println("Cities sorted lexicographical:");
for (String city : cities) {
System.out.println(city);
}
或者,如果您更喜欢使用 Streams
的紧凑 Java 8 解决方案(它基本上回落到相同的方法,尤其是相同的排序方法) :
Stream.of(city1, city2, city3).sorted().forEach(System.out::println);
请注意,String#compareTo
方法还比较字典顺序,如前所述。因此,除了使用排序算法(以巧妙的方式检查 compareTo
的结果)之外,您还可以直接对比较进行硬编码(就像您已经尝试过的那样):
String smallestCity;
if (city1.compareTo(city2) < 0 && city1.compareTo(city3) < 0) {
smallestCity = city1;
} else if (city2.compareTo(city1) < 0 && city2.compareTo(city3) < 0) {
smallestCity = city2;
} else if (city3.compareTo(city1) < 0 && city3.compareTo(city2) < 0) {
smallestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String biggestCity;
if (city1.compareTo(city2) > 0 && city1.compareTo(city3) > 0) {
biggestCity = city1;
} else if (city2.compareTo(city1) > 0 && city2.compareTo(city3) > 0) {
biggestCity = city2;
} else if (city3.compareTo(city1) > 0 && city3.compareTo(city2) > 0) {
biggestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String middleCity;
if (city1.compareTo(smallestCity) > 0 && city1.compareTo(biggestCity) < 0) {
middleCity = city1;
} else if (city2.compareTo(smallestCity) > 0 && city2.compareTo(biggestCity) < 0) {
middleCity = city2;
} else if (city3.compareTo(smallestCity) > 0 && city3.compareTo(biggestCity) < 0) {
middleCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
方法 String#compareTo
returns 0
如果元素相等,< 0
如果第一个元素较小,> 0
如果大于第二个元素 (documentation).
但如前所述,排序算法以更聪明的方式执行这些检查,更少的比较。所以你应该使用一个。
嗨,我非常不知道你会如何比较三件事。教科书例子是:
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.compareTo(city2) < 0)
System.out.println("The cities in alphabetical order are:");
System.out.println(city1);
System.out.println(city2);
else
System.out.println("The cities in alphabetical order are:");
System.out.println(city2);
System.out.println(city1);
那么你如何比较第三个并按字母顺序排列?
您可以使用内置的排序算法(或您自己的)对 字母顺序 进行排序,术语是 词典顺序 。例如,通过使用 Collections#sort
(documentation)。请注意,String
是可比较的,默认情况下使用 字典顺序 。这就是为什么您不需要显式指定顺序的原因,例如使用 Comparator
对象。
此代码段对城市进行排序并打印出来:
List<String> cities = Arrays.asList({city1, city2, city3});
Collections.sort(cities);
System.out.println("Cities sorted lexicographical:");
for (String city : cities) {
System.out.println(city);
}
或者,如果您更喜欢使用 Streams
的紧凑 Java 8 解决方案(它基本上回落到相同的方法,尤其是相同的排序方法) :
Stream.of(city1, city2, city3).sorted().forEach(System.out::println);
请注意,String#compareTo
方法还比较字典顺序,如前所述。因此,除了使用排序算法(以巧妙的方式检查 compareTo
的结果)之外,您还可以直接对比较进行硬编码(就像您已经尝试过的那样):
String smallestCity;
if (city1.compareTo(city2) < 0 && city1.compareTo(city3) < 0) {
smallestCity = city1;
} else if (city2.compareTo(city1) < 0 && city2.compareTo(city3) < 0) {
smallestCity = city2;
} else if (city3.compareTo(city1) < 0 && city3.compareTo(city2) < 0) {
smallestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String biggestCity;
if (city1.compareTo(city2) > 0 && city1.compareTo(city3) > 0) {
biggestCity = city1;
} else if (city2.compareTo(city1) > 0 && city2.compareTo(city3) > 0) {
biggestCity = city2;
} else if (city3.compareTo(city1) > 0 && city3.compareTo(city2) > 0) {
biggestCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
String middleCity;
if (city1.compareTo(smallestCity) > 0 && city1.compareTo(biggestCity) < 0) {
middleCity = city1;
} else if (city2.compareTo(smallestCity) > 0 && city2.compareTo(biggestCity) < 0) {
middleCity = city2;
} else if (city3.compareTo(smallestCity) > 0 && city3.compareTo(biggestCity) < 0) {
middleCity = city3;
} else {
throw new AssertionError("There is no strict order!");
}
方法 String#compareTo
returns 0
如果元素相等,< 0
如果第一个元素较小,> 0
如果大于第二个元素 (documentation).
但如前所述,排序算法以更聪明的方式执行这些检查,更少的比较。所以你应该使用一个。