Java运用HashMap创建一首诗
时间: 2023-11-03 07:04:14 浏览: 100
StringToHashMap:Java HashMap的扩展,它允许用户从键值对的字符串创建HashMap
HashMap储存着一个键值对,我可以用它来创建一个有趣的诗歌。以下是示例代码:
```
HashMap<String, String> poem = new HashMap<>();
poem.put("sun", "The sun shines bright in the sky");
poem.put("moon", "The moon casts a pale light");
poem.put("stars", "The stars twinkle in the night");
poem.put("ocean", "The ocean's waves crash with might");
poem.put("mountains", "The mountains stand tall and proud");
System.out.println("A Poem:");
System.out.println(poem.get("sun"));
System.out.println(poem.get("moon"));
System.out.println(poem.get("stars"));
System.out.println(poem.get("ocean"));
System.out.println(poem.get("mountains"));
```
这个程序将会输出以下这首诗:
```
A Poem:
The sun shines bright in the sky
The moon casts a pale light
The stars twinkle in the night
The ocean's waves crash with might
The mountains stand tall and proud
```
阅读全文