补上代码,有没有更简单的方法呢?? 用fore循环能做吗?
String[] arr = {"衣服", "零食", "化妆品", "鞋"};
Random r = new Random();
ArrayList<String> a1 = new ArrayList<>();
while(a1.size()<4){
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < 6; i++) {
sb.append(r.nextInt(10));
}
a1.add(sb+"");
}
//,组合的格式:(取货码,物品),并存入Map集合(Map<取货码,物品>
HashMap<String, String> h1 = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
h1.put(a1.get(i), arr[i]);
}
System.out.println(h1);
Scanner x = new Scanner(System.in);
System.out.println("nnnnnnnnnnn");
String s = x.nextLine();
while(true){
if (h1.keySet().contains(s)) {
System.out.println(h1.get(s));
break;
}else {
System.out.println("error");
break;
}
} |