package demo2;
import java.applet.AudioClip;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 创建5首诗的对象
Poem p1 = new Poem("登鹳雀楼", "王之涣", "白日依山尽,\n黄河入海流,\n欲穷千里目,\n更上一层楼。");
Poem p2 = new Poem("静夜思", "李白", "床前明月光,\n疑是地上霜,\n举头望明月,\n低头思故乡。");
Poem p3 = new Poem("悯农", "李绅", "锄禾日当午,\n汗滴禾下土,\n谁知盘中餐,\n粒粒皆辛苦。");
Poem p4 = new Poem("咏鹅", "骆宾王", "鹅鹅鹅,\n去颈向天歌,\n白毛浮绿水,\n红掌拨清波。");
Poem p5 = new Poem("春眠", "孟浩然", "春眠不觉晓,\n处处闻啼鸟,\n夜来风雨声,\n花落知多少。");
Map map = new HashMap();
map.put(p1.getName(), p1);
map.put(p2.getName(), p2);
map.put(p3.getName(), p3);
map.put(p4.getName(), p4);
map.put(p5.getName(), p5);
System.out.println("欢迎来到唐诗宋词赏析节目");
System.out.println("-------------------------------");
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("请输入您要欣赏的诗词名称:");
String name = input.next();
if (name.equals("exit")) {
System.out.println("Bye,Bye!!!");
break;
}
if (map.containsKey(name)) {
System.out.println(map.get(name));
} else {
System.out.println("您要欣赏的诗词暂时还没有");
System.out.println("是否收录?(y/n)");
String answer = input.next();
if (!"n".equals(answer)) {
System.out.print("请输入诗词名称:");
String poemName = input.next();
System.out.print("请输入诗词作者:");
String poemAuthor = input.next();
System.out.print("请输入诗词内容:");
String content = "";
for (int i = 1; i <= 4; i++) {
System.out.print("第" + i + "句:");
String poemContent = input.next();
if (i == 4) {
content += poemContent + "。\n";
} else {
content += poemContent + ",\n";
}
}
Poem newPoem = new Poem(poemName, poemAuthor, content);
map.put(poemName, newPoem);
System.out.println("新诗词录入成功!!!!");
}
}
}
}
}
HashMap()用法 范例
最新推荐文章于 2020-11-24 14:52:31 发布