
本文旨在指导开发者如何根据用户输入的多个排序条件对数据进行排序。通过使用字符串分割和 switch 语句,可以实现灵活的排序逻辑。本文将详细介绍如何处理用户输入,解析排序条件,并应用到实际的排序操作中,提供示例代码和注意事项,帮助读者更好地理解和应用该技术。
首先,我们需要获取用户输入的排序条件。这些条件通常以空格分隔的字符串形式给出。例如,用户可能输入 "1 2",表示先按名称排序,然后按身高排序。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class SortByMultipleCriteria {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("""
1. Sort by name.
2. Sort by height.
3. Sort by power(s).
4. Sort by weakness(ess).
5. Sort by origin FROM earth.
6. Sort by origin NOT from earth.
""");
System.out.println("Enter the sorting criteria (e.g., 1 2):");
String userInput = sc.nextLine().toLowerCase();
String[] userInputs = userInput.split(" ");
ArrayList<Superhero> superheroes = new ArrayList<>();
// 假设这里已经填充了 Superhero 列表
superheroes.add(new Superhero("Superman", 190, "Flight", "Kryptonite", true));
superheroes.add(new Superhero("Batman", 180, "Intelligence", "None", false));
superheroes.add(new Superhero("Wonder Woman", 183, "Strength", "Piercing Weapons", true));
ArrayList<Superhero> sortedSuperheroes = new ArrayList<>(superheroes); // 创建副本
// 应用排序
for (String input : userInputs) {
switch (input) {
case "1":
sortedSuperheroes.sort(Comparator.comparing(Superhero::getName));
break;
case "2":
sortedSuperheroes.sort(Comparator.comparing(Superhero::getHeight));
break;
case "3":
sortedSuperheroes.sort(Comparator.comparing(Superhero::getPower));
break;
case "4":
sortedSuperheroes.sort(Comparator.comparing(Superhero::getWeakness));
break;
case "5":
sortedSuperheroes.sort(Comparator.comparing(Superhero::isFromEarth));
break;
case "6":
sortedSuperheroes.sort((s1, s2) -> Boolean.compare(s2.isFromEarth(), s1.isFromEarth()));
break;
default:
System.out.println("Invalid sorting criteria: " + input);
}
}
// 打印排序结果
System.out.println("Sorted Superheroes:");
for (Superhero superhero : sortedSuperheroes) {
System.out.println(superhero);
}
}
// 假设 Superhero 类定义如下
static class Superhero {
private String name;
private int height;
private String power;
private String weakness;
private boolean fromEarth;
public Superhero(String name, int height, String power, String weakness, boolean fromEarth) {
this.name = name;
this.height = height;
this.power = power;
this.weakness = weakness;
this.fromEarth = fromEarth;
}
public String getName() {
return name;
}
public int getHeight() {
return height;
}
public String getPower() {
return power;
}
public String getWeakness() {
return weakness;
}
public boolean isFromEarth() {
return fromEarth;
}
@Override
public String toString() {
return "Superhero{" +
"name='" + name + '\'' +
", height=" + height +
", power='" + power + '\'' +
", weakness='" + weakness + '\'' +
", fromEarth=" + fromEarth +
'}';
}
}
}这段代码首先使用 Scanner 类获取用户输入,然后使用 split(" ") 方法将输入字符串分割成字符串数组。
接下来,我们需要遍历分割后的字符串数组,并使用 switch 语句根据每个字符串的值执行相应的排序操作。
for (String input : userInputs) {
switch (input) {
case "1":
//do something where it sorts by name.
break;
case "2":
//do something where it sorts by height
break;
case "3":
//do something where it sorts by power
break;
case "4":
//do somehting where it sorts by weakness
break;
case "5":
//do soemthing where it sorts by origin FROM earth (I use a boolean for it)
break;
case "6":
//do something where it sorts by origin NOT from earth (I use a booleaen for it)
break;
default:
System.out.println("Invalid sorting criteria: " + input);
}
}在每个 case 分支中,你需要实现相应的排序逻辑。可以使用 Collections.sort() 方法和自定义的 Comparator 对象来实现排序。 例如,按名称排序可以这样实现:
case "1":
sortedSuperheroes.sort(Comparator.comparing(Superhero::getName));
break;这里 Superhero::getName 是一个方法引用,它指向 Superhero 类的 getName() 方法。 Comparator.comparing() 方法使用这个方法引用创建一个 Comparator 对象,用于比较 Superhero 对象的名称。
通过使用字符串分割和 switch 语句,可以灵活地处理用户输入的多个排序条件,并将其应用到实际的排序操作中。在实现过程中,需要注意输入验证、排序顺序、数据类型和空指针异常等问题。通过合理的代码设计和优化,可以实现高效且可靠的多条件排序功能。
以上就是使用数组分割字符串以读取多个排序条件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号