
本文旨在指导开发者如何使用Java实现基于用户输入的多重条件数据排序。通过接收用户输入的多个排序标准,利用Scanner类解析输入,并结合switch语句或更高级的排序方法,实现数据的多维度排序功能。本文将提供代码示例和详细步骤,帮助读者理解和应用多重条件排序的实现方法。
首先,我们需要接收用户的排序条件输入。Scanner类是Java中用于读取用户输入的常用工具。为了能够接收多个排序条件,我们可以使用多个Scanner对象读取每一行的输入,或者使用一个Scanner对象读取一行输入,然后使用String.split()方法将其分割成多个条件。
import java.util.Scanner;
public class MultiCriteriaSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个排序条件(用空格分隔,例如:1 2):");
String userInput = sc.nextLine();
String[] userInputs = userInput.split(" ");
if (userInputs.length != 2) {
System.out.println("请输入两个排序条件,用空格分隔。");
return; // 结束程序
}
int condition1;
int condition2;
try {
condition1 = Integer.parseInt(userInputs[0]);
condition2 = Integer.parseInt(userInputs[1]);
} catch (NumberFormatException e) {
System.out.println("输入的排序条件必须是数字。");
return; // 结束程序
}
// 后续的排序逻辑将基于 condition1 和 condition2 进行
System.out.println("您选择的排序条件是:" + condition1 + " 和 " + condition2);
// 关闭 Scanner
sc.close();
}
}代码解释:
接下来,我们可以使用switch语句根据用户输入的条件进行不同的排序操作。为了实现多重排序,我们需要先按照第一个条件排序,然后在第一个条件相同的情况下,再按照第二个条件排序。
立即学习“Java免费学习笔记(深入)”;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class MultiCriteriaSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个排序条件(用空格分隔,例如:1 2):");
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.
""");
String userInput = sc.nextLine();
String[] userInputs = userInput.split(" ");
if (userInputs.length != 2) {
System.out.println("请输入两个排序条件,用空格分隔。");
return; // 结束程序
}
int condition1;
int condition2;
try {
condition1 = Integer.parseInt(userInputs[0]);
condition2 = Integer.parseInt(userInputs[1]);
} catch (NumberFormatException e) {
System.out.println("输入的排序条件必须是数字。");
return; // 结束程序
}
// 假设我们有一个 Superhero 列表
List<Superhero> superheroes = new ArrayList<>();
superheroes.add(new Superhero("Superman", 190, 100, "Kryptonite", true));
superheroes.add(new Superhero("Batman", 180, 50, "None", false));
superheroes.add(new Superhero("Spiderman", 175, 75, "None", true));
superheroes.add(new Superhero("Wonder Woman", 185, 90, "None", true));
// 使用 Comparator 进行多重排序
superheroes.sort(getComparator(condition1).thenComparing(getComparator(condition2)));
// 打印排序后的结果
System.out.println("排序后的 Superhero 列表:");
for (Superhero superhero : superheroes) {
System.out.println(superhero);
}
// 关闭 Scanner
sc.close();
}
// 根据排序条件返回相应的 Comparator
private static Comparator<Superhero> getComparator(int condition) {
switch (condition) {
case 1: // Sort by name
return Comparator.comparing(Superhero::getName);
case 2: // Sort by height
return Comparator.comparingInt(Superhero::getHeight);
case 3: // Sort by power
return Comparator.comparingInt(Superhero::getPower);
case 4: // Sort by weakness
return Comparator.comparing(Superhero::getWeakness);
case 5: // Sort by origin FROM earth
return Comparator.comparing(Superhero::isFromEarth).reversed(); // True first
case 6: // Sort by origin NOT from earth
return Comparator.comparing(Superhero::isFromEarth); // False first
default:
return Comparator.comparing(Superhero::getName); // 默认按名字排序
}
}
}
// Superhero 类
class Superhero {
private String name;
private int height;
private int power;
private String weakness;
private boolean fromEarth;
public Superhero(String name, int height, int power, String weakness, boolean fromEarth) {
this.name = name;
this.height = height;
this.power = power;
this.weakness = weakness;
this.fromEarth = fromEarth;
}
// Getters
public String getName() {
return name;
}
public int getHeight() {
return height;
}
public int 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 +
'}';
}
}代码解释:
注意事项:
通过本文,我们学习了如何使用Java实现基于用户输入的多重条件数据排序。首先,我们使用Scanner类接收用户输入的排序条件,并将其分割成多个条件。然后,我们使用switch语句或Comparator接口根据用户输入的条件进行不同的排序操作。通过这种方式,我们可以灵活地实现数据的多维度排序功能。在实际开发中,可以根据具体需求选择合适的排序算法和数据结构,以提高排序效率和代码可维护性。
以上就是使用多重条件对数据进行排序:Java教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号