package pack_01;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class Test {
/**
*@param args
*2016-9-8
*下午2:44:21
*/
public static void main(String[] args) {
String [] arr={"橙汁","雪碧","啤酒","二锅头"};
Scanner sc=new Scanner(System.in);
Random rd=new Random();
Map<Student,String> map=new HashMap<Student, String>();
for (int i = 0; i < 3; i++) {
System.out.println("请输入学生信息,格式是:姓名,年龄");
String str=sc.nextLine();
if(!(str.contains(",")||str.contains(","))){
throw new RuntimeException("你的格式有误,没有的逗号!!!");
}
if(str.contains(",")&&str.contains(",")){
throw new RuntimeException("你的格式有误,有两个不一样的逗号!!!");
}
if(str.indexOf(",")!=str.lastIndexOf(",")||str.indexOf(",")!=str.lastIndexOf(",")){
throw new RuntimeException("你的格式有误,输入了两个以上的逗号");
}
String [] strNews=str.split(",|,");
strNews[0]=strNews[0].trim();
strNews[1]=strNews[1].trim();
if((!strNews[1].matches("[0-9]+"))||new Integer(strNews[1])>128||new Integer(strNews[1])<=0){
throw new RuntimeException("你的年龄输入有误。");
}
Student stu1=new Student(strNews[0],new Integer(strNews[1]));
map.put(stu1, arr[rd.nextInt(4)]);
}
for (Student s : map.keySet()) {
if(s.getAge()<18){
while(true){
if(map.get(s).equals("啤酒")||map.get(s).equals("二锅头")){
map.put(s,arr[rd.nextInt(4)] );
}else{
break;
}
}
}
System.out.println(s+" 喝"+map.get(s));
}
}
}
|
|