Java中如何基于用户输入对内存中匹配的员工数据行排名?
解决员工数据基于用户输入匹配度排名的问题
看起来你卡在这里的核心是没搞清楚怎么把用户输入作为基准,去给每个员工计算匹配度,再用这个匹配度来排序对吧?其实Comparator完全可以做到,只要我们把用户输入的信息传递给比较器,让它在比较两个员工的时候,分别计算它们和输入的匹配得分,再比较得分高低就行。
接下来一步步帮你修正代码并实现逻辑:
第一步:修正基础代码规范和输入读取问题
首先Java类名要遵循大驼峰规范(比如把rank改成Rank),还有你用nextInt()之后直接用nextLine()会读取到空字符串——因为nextInt()不会消耗换行符,得额外加个input.nextLine()跳过才行。
第二步:设计匹配度得分计算逻辑
我们可以给每个字段设置权重(你可以根据实际需求调整),比如:
- 技能完全匹配:加30分
- 领域完全匹配:加20分
- 地点完全匹配:加20分
- 工作时长与输入差值≤5:加15分
- 期望薪资与输入差值≤5:加15分
专门写一个方法来计算单个员工和用户输入的匹配得分,逻辑会更清晰。
第三步:编写带用户输入的Comparator
把用户输入的信息传递给比较器,在compare方法里分别计算两个员工的匹配得分,再按得分降序排序(得分高的排前面)。
完整修正后的代码
import java.util.*; // 类名遵循大驼峰规范 class Rank { int hours, price; String skills, domain, location; public Rank(int hours, int price, String skills, String domain, String location) { this.hours = hours; this.price = price; this.skills = skills; this.domain = domain; this.location = location; } @Override public String toString() { return String.format("工作时长:%d,期望薪资:%d,技能:%s,领域:%s,地点:%s", hours, price, skills, domain, location); } // 计算当前员工与用户输入的匹配得分 public int calculateMatchScore(int inputHours, int inputPrice, String inputSkills, String inputDomain, String inputLocation) { int score = 0; // 技能匹配(这里用精确匹配,你也可以改成包含关系,比如用contains) if (this.skills.equalsIgnoreCase(inputSkills)) { score += 30; } // 领域匹配 if (this.domain.equalsIgnoreCase(inputDomain)) { score += 20; } // 地点匹配 if (this.location.equalsIgnoreCase(inputLocation)) { score += 20; } // 工作时长接近(差值≤5) if (Math.abs(this.hours - inputHours) <= 5) { score += 15; } // 期望薪资接近(差值≤5) if (Math.abs(this.price - inputPrice) <= 5) { score += 15; } return score; } public static void main(String[] args) { ArrayList<Rank> employees = new ArrayList<>(); employees.add(new Rank(20, 30, "java,oracle", "fin", "hyd")); employees.add(new Rank(30, 40, "oracle", "fin", "hyd")); employees.add(new Rank(10, 40, "oracle", "fin", "hyd")); employees.add(new Rank(50, 40, "java", "fin", "chn")); System.out.println("未排序的员工列表:"); for (Rank emp : employees) { System.out.println(emp); } Scanner input = new Scanner(System.in); System.out.println("\n请输入筛选条件:"); System.out.print("工作时长:"); int inputHours = input.nextInt(); System.out.print("期望薪资:"); int inputPrice = input.nextInt(); // 跳过nextInt后的换行符,避免后续nextLine读空 input.nextLine(); System.out.print("技能:"); String inputSkills = input.nextLine(); System.out.print("领域:"); String inputDomain = input.nextLine(); System.out.print("地点:"); String inputLocation = input.nextLine(); // 使用Comparator排序,根据匹配得分降序 Collections.sort(employees, new Comparator<Rank>() { @Override public int compare(Rank emp1, Rank emp2) { int score1 = emp1.calculateMatchScore(inputHours, inputPrice, inputSkills, inputDomain, inputLocation); int score2 = emp2.calculateMatchScore(inputHours, inputPrice, inputSkills, inputDomain, inputLocation); // 降序排序,得分高的在前;如果要升序就反过来写score1 - score2 return Integer.compare(score2, score1); } }); System.out.println("\n按匹配度排序后的员工列表:"); for (Rank emp : employees) { int score = emp.calculateMatchScore(inputHours, inputPrice, inputSkills, inputDomain, inputLocation); System.out.println(emp + " | 匹配得分:" + score); } input.close(); } }
关键逻辑说明
- 匹配得分计算:
calculateMatchScore方法把每个员工字段和用户输入对比,按规则累加得分,你可以随时调整权重或者匹配规则(比如技能改成包含用户输入的技能,用this.skills.contains(inputSkills))。 - Comparator的使用:这里用匿名内部类创建比较器,在
compare方法里分别计算两个员工的匹配得分,再通过Integer.compare(score2, score1)实现降序排序。 - 输入处理:解决了
nextInt后nextLine读空的问题,通过额外的input.nextLine()跳过换行符。
这样运行代码后,输入用户的条件,就能得到按匹配度从高到低排序的员工列表啦!
内容的提问来源于stack exchange,提问作者kalyan vedagiri
相关产品推荐
相关产品推荐

