ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java基础练习题

2022-01-29 18:32:00  阅读:141  来源: 互联网

标签:练习题 goods java String int 基础 println new public


1.需求:定义一个方法,找出int数组中,最大值的索引下标

 //定义一个方法 找出int数组中最大值的索引下标
        int [] l1 = {23,45,56,67,78545};
        Max(l1);//4
        int [] l2 = {34,65656,6778,89};
        Max(l2);//1
    }
    public  static  void Max(int [] arr ) {
        int maxIndex = 0; //最大值的索引的一个变量
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[maxIndex]) {
                maxIndex = i;
            }
        }
        System.out.println(maxIndex);
    }

2.在指定的int数组中找出指定的数据的下标

    public static void main(String[] args) {
      //  在指定的int数组中找出指定的数据的下标
        int [] arr = {34,45,56,677,5,78};
        int index = indexOf(arr,5);
        System.out.println(index);
    }
    public  static  int  indexOf(int [] arr, int found){
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == found){
                index = i;
                break;
            }
        }
        return index;
    }

3.List集合斗地主案例

public class Test1 {
    public static void main(String[] args) {
        /**
         * List集合案例:按照斗地主的规则,完成洗牌发牌的动作。
         * 具体规则:
         *      使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张,最后三张留作底牌;
         *      案例分析:准备牌:花色与数字集合组成一张牌。使用Collections类shuffle方法进行随机排序;
         *      发牌:将牌依次发给三个人,留下底牌三张
         *      看牌:直接打印每人手里的牌,和底牌
         */
        //1.创建一个集合数组当成牌盒子
        List<String> Pai = new ArrayList<>();

        //2.准备牌
        String [] str = {"♥️" , "♠️" ,"♣️" ,"♦️"};
        for (int i = 0; i < str.length; i++) {
            for (int j = 2; j < 11 ; j++) {
                Pai.add(str[i] + j+ " ");
            }
        }
//        for (String ss:Pai) {
//            System.out.println(ss);
//        }
        String [] str1 = {"A","J","Q","K"};
        for (int i = 0; i < str.length; i++) {
            for (int j = 0; j < str1.length; j++) {
                Pai.add(str[i] + str1[j]);
            }
        }
//        for (String ss:Pai) {
//            System.out.println(ss);
//        }

        Pai.add("大王");
        Pai.add("小王");
        //打错顺序
        Collections.shuffle(Pai);
//        for (String ss:Pai) {
//            System.out.println(ss);
//        }

        //3.定义三个玩家
        ArrayList<String> play1 = new ArrayList<>();
        ArrayList<String> play2 = new ArrayList<>();
        ArrayList<String> play3 = new ArrayList<>();
        ArrayList<String> diPai = new ArrayList<>();

        //4.遍历牌
        for (int i = 0; i < Pai.size(); i++) {
            String card = Pai.get(i);
            //  留三张底牌
            if ( i >= 51){  //余三张存到底牌中
                diPai.add(card);
            }else {
                //发给三个人 集合  依次发牌
                if (i % 3 == 0){
                    play1.add(card);
                }else if (i % 3 == 1){
                    play2.add(card);
                }else {
                    play3.add(card);
                }
            }
        }
        //看牌
        System.out.println("玩家1"+play1);
        System.out.println("玩家2"+play2);
        System.out.println("玩家3"+play3);
        System.out.println("底牌" + diPai);
    }
}

4.需求:4个景点 80人投票 统计投票人数

public class Test5 {
    public static void main(String[] args) {
        //1.把80个学生选择的数据拿进来
        String [] selects  = {"A","B","C","D"};
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i <80 ; i++) {
            sb.append(selects[r.nextInt(selects.length)]);
        }
        //2.定义一个Map集合记录最终结果
        Map<Character,Integer> infos = new HashMap<>();
        //3.遍历80个学生选择的数据
        for (int i = 0; i < sb.length(); i++) {
            //4.提取当前选择景点的字符
            char ch = sb.charAt(i);
            //5.判断Map集合中是否存在这个键
            if(infos.containsKey(ch)){
                infos.put(ch,infos.get(ch)+1);
            }else {
                infos.put(ch,1);
            }
        }
        //4.输出集合
        System.out.println(infos);
    }
}

5.生产者消费者模式

public class Test3 {
    public static void main(String[] args) throws InterruptedException {
            // 生产消费者模式
        Goods goods =new Goods("保时捷", 67.8,true);   //true需要被生产
        Customer customer  =new Customer(goods);
        Producer producer = new Producer(goods);
        new Thread(customer).start();
        new Thread(producer).start();
    }
}

//1.共享资源对象 两个线程同时操作
class Goods{
    private  String name;//商品名字
    private double price;//商品价格
    private boolean shouldProduct;//没有商品为true

    public Goods() {
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", shouldProduct=" + shouldProduct +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean isShouldProduct() {
        return shouldProduct;
    }

    public void setShouldProduct(boolean shouldProduct) {
        this.shouldProduct = shouldProduct;
    }

    public Goods(String name, double price, boolean shouldProduct) {
        this.name = name;
        this.price = price;
        this.shouldProduct = shouldProduct;
    }
}

//2 .写两个线程
//  消费者线程
class Customer implements Runnable{
    //由于两个线程需要共享一个资源
    private Goods goods;
    public Customer(Goods goods){
        this.goods = goods;
    }
    @Override
    public void run() {
        //消费多个 无限制消费
        while (true){
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            synchronized (goods){
                //  执行不需要生产的代码
                if (!goods.isShouldProduct()){
                    System.out.println("消费者购买了:" + goods.getName()+ ",价格为:" + goods.getPrice());
                    //购买完以后商品没了 需要标记为true
                    goods.setShouldProduct(true);
                    //唤醒生产者
                    goods.notify();
                }else{
                    //需要生产  消费者需要等待
                    try {
                        //  没有商品 需要生产 消费者进入阻塞状态
                        goods.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

//  生产者线程
class Producer  implements Runnable{
    private Goods goods;
    public Producer(Goods goods){
        this.goods = goods;
    }
    @Override
    public void run() {
        int count = 0;
        while (true){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (goods){
                if (goods.isShouldProduct()){
                    if (count  %2 ==0 ){
                        goods.setName("奥迪R9");
                        goods.setPrice(54.9);
                    }else {
                        goods.setName("五菱");
                        goods.setPrice(16.8);
                    }
                    //  生产完以后 标记生产状态为false
                    goods.setShouldProduct(false);
                    System.out.println("生产者生产了:" + goods.getName()+",价格为:"+goods.getPrice());
                    count++;
                    //唤醒消费者 让他去消费
                    goods.notify();
                }else {
                    //有车不需要生产 生产者等待
                    try {
                        goods.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

标签:练习题,goods,java,String,int,基础,println,new,public
来源: https://blog.csdn.net/m0_64563365/article/details/122610101

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有