}
//根据年龄获取随机值
public static String randomValue(int age,String[] str){
Random rd=new Random();
int index=rd.nextInt(4);
if (age<18) {
//只能喝饮料,
while(str[index].equals("啤酒")||str[index].equals("白酒")){
index=rd.nextInt(4);
}
}else {
//否则可以和所有饮料
index=rd.nextInt(4);
}
return str[index];
}
}
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
} 作者: wyq0627java11 时间: 2016-8-17 21:23
public class Drink {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("person-info.txt");
String[] str = { "奶昔","啤酒","加多宝","白酒" };
Random ra = new Random();
Map<Student, String> has = new HashMap<>();
for (int i = 0; i < 3; i++) {
System.out.println("请输入person的信息,格式为:姓名,年龄");
String[] st = new Scanner(System.in).nextLine().split(",");
Student p = new Student(st[0], Integer.parseInt(st[1]));
int num = ra.nextInt(str.length);
while (p.getAge() < 18 && (str[num].equals("白酒") || str[num].equals("啤酒"))) {
num = ra.nextInt(str.length);
}
has.put(p, str[num]);
fw.write(p+":"+str[num]);