public static void main(String[] args) throws IOException {
Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if (!line.contains(",")) {
System.out.println("incorrect input string");
continue;
}
String name = line.split(",")[0];
String age = line.split(",")[1];
age = age.trim();
int ageInt = -1;
try {
ageInt = Integer.parseInt(age);
} catch (NumberFormatException e) {
System.out.println("age not a number");
continue;
}
ageInt = ageInt - ageInt % 5;
List<String> names = result.get(ageInt);
if (names == null) {
names = new ArrayList<String>();
}
names.add(name);
result.put(ageInt, names);
printResult(result);
}
}
private static void printResult(Map<Integer, List<String>> result) {
List<Integer> ages = new ArrayList<Integer>();
ages.addAll(result.keySet());
Collections.sort(ages);
for (Integer integer : ages) {
List<String> name2 = result.get(integer);
System.out.println(integer + " - " + (integer + 5) + " : ");
for (String s : name2) {
System.out.println(" " + s);
}
}
}
什麼是問題/你有什麼嘗試? – Reimeus
'HashMap>'是你的朋友。您至少應該發佈您嘗試的代碼,並指定問題所在。 –
membersound
至少嘗試好友... – CoderNeji