输入年份,输出该年的世界杯冠军是哪支球队。否则输出:没有举办世界杯。 读入球队名字,输出该球队夺冠的年份。 历届世界杯冠军 :

在这里插入图片描述


import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

class Test{
	public static void main(String[] args) {
	HashMap<Integer, String> m = new HashMap<Integer,String>();
	m.put(1930,"乌拉圭");
	m.put(1934,"意大利");
	m.put(1938,"意大利");
	m.put(1950,"乌拉圭");
	m.put(1954,"西德");
	m.put(1958,"巴西");
	Scanner input = new Scanner(System.in);
//	System.out.print("请输入年份:");
//	int in = input.nextInt();
//	if(m.containsKey(in)) {
//	String g = m.get(in);
//	System.out.println("这一年的冠军是:"+g);
//	}else {
//		System.out.println("这一年没有举办世界杯!");
//	}
	System.out.println("请输入球队:");
	String s = input.next();
	if(m.containsValue(s)) {
		Set<Entry<Integer, String>> se = m.entrySet();
		for (Entry<Integer, String> e : se) {
			if(e.getValue().equals(s)) {
				System.out.println("这支球队夺冠的年份为:"+e.getKey());
			}
		}
	}else {
		System.out.println("这支球队没有世界冠军!!!");
	}
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用如下的代码来实现: ```java import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class WorldCupChampion { public static void main(String[] args) { Map<Integer, String> champions = new HashMap<>(); champions.put(1930, "乌拉圭"); champions.put(1934, "意大利"); champions.put(1938, "意大利"); champions.put(1950, "乌拉圭"); champions.put(1954, "西德"); champions.put(1958, "巴西"); champions.put(1962, "巴西"); champions.put(1966, "英格兰"); champions.put(1970, "巴西"); champions.put(1974, "西德"); champions.put(1978, "阿根廷"); champions.put(1982, "意大利"); champions.put(1986, "阿根廷"); champions.put(1990, "西德"); champions.put(1994, "巴西"); champions.put(1998, "法国"); champions.put(2002, "巴西"); champions.put(2006, "意大利"); champions.put(2010, "西班牙"); champions.put(2014, "德国"); champions.put(2018, "法国"); Scanner scanner = new Scanner(System.in); System.out.print("请输入一个年份:"); int year = scanner.nextInt(); String champion = champions.get(year); if (champion != null) { System.out.println(year + "世界杯冠军是:" + champion); } else { System.out.println(year + "没有举办世界杯。"); } } } ``` 以上代码中,我们使用了一个 `HashMap` 来存储历届世界杯冠军。然后从命令行读入一个年份,并且查找该年份冠军。如果找到了,就输出冠军的名称;如果没有找到,就输出相应的提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thone-LX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值