io流的编程题

现有坚果100份。分别采用官网和实体店的形式进行售卖!卖出的间隔时间为500毫秒!

	a、请使用多线程模拟卖坚果
	b、为线程加上名称,例如:
		官网卖出第1份,还剩余99份
		实体店卖出第2份,还剩余98份
		实体店卖出第3份,还剩余97份
		官网卖出第4份,还剩余96份
		...
	c、统计官网和实体店分别卖出的总数,例如:
		官网共卖出了:45份
		实体店共卖出了:55份
public class word01 {
    private static int nutCount = 100;  // 坚果总数
    private static int webCount = 0;  // 官网卖出数量
    private static int entityCount = 0;  // 实体店卖出数量

    public static void main(String[] args) {
        Thread webThred = new Thread(new Runnable() {
            @Override
            public void run() {

                while (nutCount > 0) {
                    synchronized (word01.class) {
                        nutCount--;
                        webCount++;
                        System.out.println("官网卖出去第" + (100 - nutCount) + "份,还剩余" + nutCount + "份");
                    }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                System.out.println("官网售出"+webCount+"份");
            }
        }, "官网");
        Thread entityThred = new Thread(new Runnable() {
            @Override
            public void run() {

                while (nutCount > 0) {
                    synchronized (word01.class) {
                        nutCount--;
                        entityCount++;
                        System.out.println("实体店卖出去第" + (100 - nutCount) + "份,还剩余" + nutCount + "份");
                    }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                System.out.println("实体店售出"+entityCount+"份");
            }
        }, "实体店");

        webThred.start();
        entityThred.start();


    }
}

请使用实现Runnable接口的方式开启两个线程

	(1)第一个线程的名字设置为:a   第二个线程的名字设置为:b
	(2)第一个线程里面实现计算1+2+3+4+....+100的和
	(3)第二个线程里面实现计算1+2+3+4+....+200的和
	程序最终打印结果:
			a:5050
			b:20100   (a和b的打印顺序不作要求)
public class word02 {
    /**
     * 请使用实现Runnable接口的方式开启两个线程
     * (1)第一个线程的名字设置为:a   第二个线程的名字设置为:b
     * (2)第一个线程里面实现计算1+2+3+4+....+100的和
     * (3)第二个线程里面实现计算1+2+3+4+....+200的和
     * 程序最终打印结果:
     * a:5050
     * b:20100   (a和b的打印顺序不作要求)
     */
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 100;
                int num = 0;
                    for (int j = 100; j > 0; j--) {
                        num += j;
                    }
                System.out.println("a:"+num);
            }
        }, "a");
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 200;
                int num = 0;
                for (int j = 200; j > 0; j--) {
                    num += j;
                }
                System.out.println("b:"+num);
            }
        }, "b");
        thread.start();
        thread1.start();
    }
}

通过反射技术,在这个泛型为Integer的ArrayList中存放一个String类型的对象

有如下代码:
ArrayList list = new ArrayList();
list.add(100);
list.add(200);
通过反射技术,在这个泛型为Integer的ArrayList中存放一个String类型的对象

public class word03 {
    /**
     * 有如下代码:
     * 	ArrayList<Integer> list = new ArrayList<Integer>();
     * 	list.add(100);
     * 	list.add(200);
     *
     * 	通过反射技术,在这个泛型为Integer的ArrayList中存放一个String类型的对象
     */
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(100);
    	list.add(200);
        System.out.println(list);
        //先获取到目标对象的类
        Class<? extends ArrayList> aClass = list.getClass();
        //再获取的到需要的方法
        Method method = aClass.getMethod("add", Object.class);

        method.invoke(list,"abc");
        System.out.println(list);
    }
}

某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票

,假设公司有100个员工,利用多线程模拟年会入场过程,并分别统计每个入口入场的人数,以及每个员工拿到的彩票的号码。线程运行后打印格式如下:
编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是: [17, 24, 29, 30, 31, 32, 07]
编号为: 1 的员工 从后门 入场! 拿到的双色球彩票号码是: [06, 11, 14, 22, 29, 32, 15]
//…
从后门入场的员工总共: 13 位员工
从前门入场的员工总共: 87 位员工

public class word04 {
    private static int people = 100;  // 人总数

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (people > 0) {
                    synchronized (word04.class) {
                        people--;
                        System.out.println("编号为: " + (100 - people) + " 的员工 从" + Thread.currentThread().getName() + " 入场! 拿到的双色球彩票号码是: " + DoubleColorBallUtil.create());
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "前门");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (people > 0) {
                    synchronized (word04.class) {
                        people--;
                        System.out.println("编号为: " + (100 - people) + " 的员工 从" + Thread.currentThread().getName() + " 入场! 拿到的双色球彩票号码是: " + DoubleColorBallUtil.create());
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "后门");
        thread1.start();
        thread2.start();
    }
}

DoubleColorBallUtil类(工具类)

public class DoubleColorBallUtil {
    //产生双色球的代码
    public static String create() {
        String[] red = {"01","02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16","17","18","19","20","21","22","23",
                "24","25","26","27","28","29","30","31","32","33"};
			//创建红球
			for(int i=0;i<red.length;i++) {
				char[] ch = {'0','0'};
				String s = Integer.toString(i+1);//"1"
				char[] num = s.toCharArray();//{'1'}
				System.arraycopy(num, 0, ch, ch.length-num.length, num.length);
				String ball = new String(ch);
				red[i] = ball;
			}

        //System.out.println(Arrays.toString(red));//打印01-33
        //创建蓝球
        String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
        boolean[] used = new boolean[red.length];
        Random r = new Random();
        String[] all = new String[7];
        for(int i = 0;i<6;i++) {
            int idx;
            do {
                idx = r.nextInt(red.length);//0-32
            } while (used[idx]);//如果使用了继续找下一个
            used[idx] = true;//标记使用了
            all[i] = red[idx];//取出一个未使用的红球
        }
        all[all.length-1] = "99";
        //System.out.println(Arrays.toString(all));
        Arrays.sort(all);
        all[all.length-1] = blue[r.nextInt(blue.length)];
        return Arrays.toString(all);
    }
}

项目根路径下有个questions.txt文件内容如下:

	5+5
	150-25
	155*155
	2555/5
要求:读取内容计算出结果,将结果写入到results.txt文件中
	5+5=10
	//....
思考:如果说读取questions.txt文件的内容,算出计算结果,再写入questions.txt文件,即读和写的操作时针对同一个文件,应该如何操作
public class word05 {
    public static void main(String[] args) throws IOException {
        //创建BufferedWriter写入文本文件
        //BufferedWriter bw = new BufferedWriter(new FileWriter("E:/file/fil2/text.txt", true));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:/file/fil2/results.txt"));
        //创建BufferedReader读取文件
        BufferedReader br = new BufferedReader(new FileReader("E:/file/fil2/text.txt"));
        String line = null;
        //循环写入
        while ((line = br.readLine()) != null) {
            //计算结果,找到运算符,找到参与运算的两个数据
            //将字符串,转为成字符数组
            char[] chars = line.toCharArray();
            //定义变量,记录操作运算符
            int operatorIndex = 1;
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                //满足条件就是我们要找的运算符
                if (!(c >= '0' && c <= '9')) {
                    operatorIndex = i;
                    //System.out.println("operatorIndex = " + operatorIndex);
                    System.out.println(chars[operatorIndex]);
                    break;
                }
            }
            //获取操作运算符和两边参与运损的数据
            char aChar = chars[operatorIndex];
            String qian = line.substring(0, operatorIndex);
            String hou = line.substring(operatorIndex + 1);
            int fist = Integer.parseInt(qian);
            int last = Integer.parseInt(hou);
            //System.out.println(fist);
            //System.out.println(last);

            //定义变量,用于保存计算结果
            int result = 0;
            switch (aChar) {
                case '+':
                    result = fist + last;
                    break;
                case '-':
                    result = fist - last;
                    break;
                case '*':
                    result = fist * last;
                    break;
                case '/':
                    result = fist / last;
            }
            System.out.println(line + "=" + result);
            bw.write(line + "=" + result);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}

将集合写到文件.txt,再读出来

1.定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法	
2.键盘录入6个学员信息(录入格式:张三,25,男),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
3.将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
4.读取D:\\StudentInfo.txt文件中的ArrayList对象并遍历打印
5.对ArrayList集合中的6个学生对象进行去重,将去重后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)

张三,23,男
张三,23,男
李四,24,女
王五,25,男
赵六,26,女
周七,27,男
public class word06 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> arrs = new ArrayList<>();
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:/file/fil2/StudentInfo.txt"));
        BufferedReader br = new BufferedReader(new FileReader("E:/file/fil2/StudentInfo.txt"));
        for (int i = 0; i < 6; i++) {
            System.out.println("请输入学生信息-名字");
            String name = scanner.next();
            System.out.println("请输入学生信息-性别");
            String gender = scanner.next();
            System.out.println("请输入学生信息-年龄");
            int age = scanner.nextInt();
            Student student = new Student(name, gender, age);
            arrs.add(student);
        }
        System.out.println(arrs);
        bw.write(String.valueOf(arrs));
        bw.close();
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

在当前项目下有一个student.txt 文件里的数据如下:

	张三丰=21
	灭绝师太=38
	柳岩=28
	刘德华=40
	老鬼=36
	皱市明=21
说明:(张三丰=21 其中 张三丰:姓名,21:年龄)
a.将文件里的数据读取出来,打印到控制台上 (为了方便操作可以定义Student类,属性name、age)
	分析:
		1.创建输入流对象,关联student.txt
		2.创建Properties集合,加载输入流对象
		3.遍历Properties集合
b.循环显示菜单“请选择您要进行的操作:1.根据姓名查询年龄 2.根据年龄查询姓名 3.退出”
	举例:
	用户输入1
		控制台打印:请输入姓名
		用户输入:张三丰
		控制台打印:张三丰年龄为21岁
		如果输入的姓名不存在,则循环输入,直到存在为止
	用户输入2
		控制台打印:请输入年龄
		用户输入:21
		查询结果:年龄为21岁的人员信息如下:
		张三丰(注意:年龄为21岁的可能不止一个)
		如果输入的年龄不存在,则继续输入,直到存在为止
	用户输入3
		退出系统打印:欢迎下次再来,拜拜
public class word07 {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader("E:/file/fil2/student.txt"));
        ArrayList<Student02> Properties = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            //使用分割split()方法
            String[] split = line.split("=");
            Properties.add(new Student02(split[0], split[1]));
            //System.out.println(line);
        }
        Properties.forEach(pro -> System.out.println(pro));
        /**
         * 分析:
         *  1.创建输入流对象,关联student.txt
         *  2.创建Properties集合,加载输入流对象
         *  3.遍历Properties集合
         */
        while (true) {
            System.out.println("请选择您要进行的操作:1.根据姓名查询年龄 2.根据年龄查询姓名 3.退出");
            int select = scanner.nextInt();
            System.out.println("用户输入" + select);

            /**
             * 控制台打印:请输入年龄
             *   用户输入:21
             *   查询结果:年龄为21岁的人员信息如下:
             *   张三丰(注意:年龄为21岁的可能不止一个)
             *   如果输入的年龄不存在,则继续输入,直到存在为止
             */
            switch (select) {
                case 1:
                    one(Properties);
                    break;
                case 2:
                    two(Properties);
                    break;
                case 3:
                    System.out.println("拜拜");
                    System.exit(0);
            }
        }

    }

    private static void two(ArrayList<Student02> Properties) {
        while (true) {
            System.out.println("请输入年龄");
            int age1 = scanner.nextInt();
            String age = String.valueOf(age1);
        /*for (int i = 0; i < Properties.size(); i++) {
            String age = String.valueOf(age1);
            if (Properties.get(i).getAge().equals(age)) {
                System.out.println(Properties.get(i).getName());
            }
        }*/
            for (Student02 student02 : Properties) {
                if (student02.getAge().equals(age)) {
                    System.out.println(student02.getName());
                    return;
                }
            }
        }
    }

    private static void one(ArrayList<Student02> Properties) {
        while (true) {
            System.out.println("请输入姓名");
            String name = scanner.next();
            /*for (int i = 0; i < Properties.size(); i++) {
                      if (Properties.get(i).getName().equals(name)) {
                      System.out.println(Properties.get(i).getAge());
                      }
            }*/
            for (Student02 property : Properties) {
                if (property.getName().equals(name)) {
                    System.out.println(property.getAge());
                    return;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值