1、利用Map接口,定义两个班的成绩表: 1班 2班 Zhang 92 Meng 93 Wang 94 Sun 92 Li 93.5 Guo 95 Zhao 98 Yang 99 Sun 100 He 99.5 并用Map的形式打印输出两个班的Key-Value值;输出成绩为100的Map映射;
时间: 2024-02-15 11:05:55 浏览: 56
利用Map集合储存并打印数据库信息
5星 · 资源好评率100%
下面是Java代码实现:
```java
import java.util.HashMap;
import java.util.Map;
public class ScoreTable {
public static void main(String[] args) {
Map<String, Double> class1 = new HashMap<>();
class1.put("Zhang", 92.0);
class1.put("Wang", 94.0);
class1.put("Li", 93.5);
class1.put("Zhao", 98.0);
class1.put("Sun", 100.0);
Map<String, Double> class2 = new HashMap<>();
class2.put("Meng", 93.0);
class2.put("Sun", 92.0);
class2.put("Guo", 95.0);
class2.put("Yang", 99.0);
class2.put("He", 99.5);
System.out.println("1班:");
for (Map.Entry<String, Double> entry : class1.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("2班:");
for (Map.Entry<String, Double> entry : class2.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
Map<String, Double> score100 = new HashMap<>();
for (Map.Entry<String, Double> entry : class1.entrySet()) {
if (entry.getValue() == 100.0) {
score100.put(entry.getKey(), entry.getValue());
}
}
for (Map.Entry<String, Double> entry : class2.entrySet()) {
if (entry.getValue() == 100.0) {
score100.put(entry.getKey(), entry.getValue());
}
}
System.out.println("成绩为100的Map映射:");
for (Map.Entry<String, Double> entry : score100.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
输出结果为:
```
1班:
Zhang: 92.0
Wang: 94.0
Li: 93.5
Zhao: 98.0
Sun: 100.0
2班:
Meng: 93.0
Sun: 92.0
Guo: 95.0
Yang: 99.0
He: 99.5
成绩为100的Map映射:
Sun: 100.0
```
阅读全文