package com.itheima.dzAndroid;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) throws IOException {
String[] str = { "奶昔", "加多宝", "啤酒", "白酒" };
HashMap<Person, String> hm = new HashMap<>();
Scanner sc = new Scanner(System.in);
for (int i = hm.size(); i < 3 ; i++) {
System.out.println("请输入你的信息,格式:姓名,年龄"); // "zhangsan" "13"
String[] message = sc.nextLine().split(",");
Person p = new Person(message[0], Integer.parseInt(message[1]));
// 给用户录入的人,设置一种饮料即可
int num = (int) (Math.random() * str.length);
while (p.getAge() < 18 && (str[num].equals("白酒") || str[num].equals("啤酒"))) {
num = (int) (Math.random() * str.length);
}
hm.put(p, str[num]);
/*while(true){
int num = (int) (Math.random() * str.length);
if (p.getAge() < 18 && (str[num].equals("白酒") || str[num].equals("啤酒"))) {
continue;
} else {
hm.put(p, str[num]);
break;
}
}*/
}
BufferedWriter bw = new BufferedWriter(new FileWriter("person_info.txt"));
for (Person key : hm.keySet()) {
bw.write(key.toString() + ":" + hm.get(key));
bw.newLine();
}
bw.close();
}
}
|
|