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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package com.itheima2_mumu;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

/*
* 题目一:
         创建一个Student类,属性有2个,分别是:name(String类型),age(int类型)
         定义一个String数组str[]={“橙汁”,”雪碧”,”啤酒”,”二锅头”}用来代表喝的饮料
         创建3个Student对象,从控制台获取属性值,输入格式为:姓名,年龄。
        (利用有参构造赋值),将这3个对象存入到Map集合中(map<学生,要喝的饮料>),其中key是Student对象,
        对应的value值需要随机(random)从str数组中获取,
        (如果Student对象的年龄不满18岁则不能喝酒,
        如果获取到白酒或者啤酒则必须继续获取,直到获取到其它饮料为止)
        将map中,所有信息写到当前项目Student_info.txt下
*/
public class One {
        public static void main(String[] args) throws IOException {
                HashMap<Student, String> hm = method();
                System.out.println(hm);
                write(hm);
        }


        private static void write(HashMap<Student, String> hm)
                        throws FileNotFoundException, IOException {
                FileOutputStream fos = new FileOutputStream("Student_info.txt");
                Set<Entry<Student,String>> entry = hm.entrySet();
                Iterator<Entry<Student,String>> ite = entry.iterator();
                while(ite.hasNext()) {
                        Entry<Student, String> entry2 = ite.next();
                        Student key = entry2.getKey();
                        String value = entry2.getValue();
                        fos.write((key + "\t" + value + "\r\n").getBytes());
                }
               
                System.out.println("Done!");
        }


        public static HashMap<Student, String> method() {
                String str[] = {"橙汁","雪碧","啤酒","二锅头"};
                Scanner sc = new Scanner(System.in);
                HashMap<Student, String> hm = new HashMap<>();
                Random random = new Random();
                for(int i = 0; i < 3 ; i++) {
                        System.out.println("请输入第"+(i+1)+"个人的姓名:");
                        String name = sc.next();
                        System.out.println("请输入他的年龄:");
                        int age = sc.nextInt();
                        Student student = new Student(name, age);
                        int ra = random.nextInt(4);
                        hm.put(student, str[ra]);
                }
                System.out.println(hm);
                System.out.println("================================");
                Set<Student> key = hm.keySet();
                for(Student keys : key) {
                        if(keys.age < 18 ) {
                                if(hm.get(keys).equals("啤酒") || hm.get(keys).equals("二锅头")) {
                                        while(true) {
                                                String kind = str[random.nextInt(4)];
                                                if(!kind.equals("啤酒") && !kind.equals("二锅头")) {
                                                        hm.put(keys, kind);
                                                        System.out.println(hm);
                                                        System.out.println("==============================");
                                                        break;
                                                }
                                               
                                        }
                                       
                                }
                        }
                }
                return hm;
        }
}


3 个回复

倒序浏览
666666666666666666666
回复 使用道具 举报
哈哈可以可以
回复 使用道具 举报
厉害厉害~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马