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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 自信boy 中级黑马   /  2016-8-17 20:48  /  618 人查看  /  3 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

题目:创建一个person类,属性有name和age
有一个字符串数组String[] str={"奶昔","啤酒","加多宝","白酒"};
新建3个person对象,从键盘接收数据,格式为姓名,年龄
根据年龄随机选择一种饮料,小于18岁的只能选奶昔和加多宝,而>=18岁不受限制
最后将3个person对象和喜爱的饮料写入到person-info.TXT文件中,格式为"姓名:   年龄:   饮料:“

3 个回复

倒序浏览

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;

public class Test111 {
public static void main(String[] args) throws Exception {
        String[] str={"奶昔","啤酒","加多宝","白酒"};
        HashMap<Person, String> map=new HashMap<>();
        Scanner scanner=new Scanner(System.in);
        while (map.size()<4) {
                try {
                        System.out.println("请输入第个"+(map.size()+1)+"学生的信息,格式为姓名,年龄");
                        String mes=scanner.nextLine();
                        Person person=new Person(mes.split(",")[0], Integer.parseInt(mes.split(",")[1]));
                        map.put(person, randomValue(person.getAge(), str));
                } catch (Exception e) {
                        System.out.println("输入信息不合法,请对照格式输入");
                        //e.printStackTrace();
                }
        }
       
        BufferedWriter writer=new BufferedWriter(new FileWriter("person-info.txt"));
        for (Entry<Person, String> entry : map.entrySet()) {
                String info="姓名:"+entry.getKey().getName()+":"+entry.getKey().getAge()+":"
        +entry.getValue();
                writer.write(info);
                writer.newLine();
        }
        writer.close();
       
}
//根据年龄获取随机值
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;
        }
       
}
回复 使用道具 举报
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]);
               
                }
                fw.close();
        }
}

person类自己写吧
回复 使用道具 举报
基础还没上完 这题简单不简单
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马