A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 15638876427 中级黑马   /  2016-9-8 22:13  /  494 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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));
        }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马