java中可变参数和简单游戏

可变参数:

就是一种特殊形参,定义在方法,构造器的形参列表中,格式是:数据类型...参数名称

可变参数的好处:

灵活的接收数据

特点:可以不传数据给它,可以传一个数据或者多个数据给它,也可以传一个数组给它

可变参数的注意事项:

可变参数在方法内部就是一个数组

一个形参列表中的可变参数只有一个

可变参数必须在形参列表的最后面

java 复制代码
public class test {
    public static void main(String[] args) {
        ttt();//可以不传数据

        ttt(1);//可以传一个数据
        ttt(1,2,3);//可以传多个数据

        ttt(new int[]{1,2,3});//可以传一个数组
    }
    public static void ttt(int...nums)
    {
        //可变参数在方法内部是一个数组
        System.out.println(Arrays.toString(nums));
    }
    //只能有一个可变参数,且必须在形参列表的最后
    public static void ppp(int age,int...s)
    {}
}

Collections类:

是一个用来操作集合的工具类

public static <T> boolean addAll(Collection<? super T> c, T... elements)

Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.

往集合中批量添加数据

public static void shuffle(List<?> list)

Randomly permutes the specified list using a default source of randomness.

打乱List集合中的元素顺序

void sort(List<T> list)

对List集合中的元素进行升序排序

public static <T> void sort(List<T> list, Comparator<? super T> c)

Sorts the specified list according to the order induced by the specified comparator.

对List集合中的元素,按照比较器对象指定的规则进行排序

java 复制代码
public class test {
    public static void main(String[] args) {
        //1 addAll
        ArrayList<String>list=new ArrayList<>();
        Collections.addAll(list,"java1","java2","java3");
        System.out.println(list);

        //2:shuffle
        Collections.shuffle(list);
        System.out.println(list);//[java2, java3, java1]

        //sort,默认升序
        Collections.sort(list);
        System.out.println(list);//[java1, java2, java3]

        //sort排序自定义对象
        ArrayList<Student>list1=new ArrayList<>();
        list1.add(new Student("aa",10));
        list1.add(new Student("bb",90));
        list1.add(new Student("cc",22));
        Collections.sort(list1, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()-o2.getAge();//按照年龄升序
            }
        });
        Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
        System.out.println(list1);
    }
}public class test {
    public static void main(String[] args) {
        //1 addAll
        ArrayList<String>list=new ArrayList<>();
        Collections.addAll(list,"java1","java2","java3");
        System.out.println(list);

        //2:shuffle
        Collections.shuffle(list);
        System.out.println(list);//[java2, java3, java1]

        //sort,默认升序
        Collections.sort(list);
        System.out.println(list);//[java1, java2, java3]

        //sort排序自定义对象
        ArrayList<Student>list1=new ArrayList<>();
        list1.add(new Student("aa",10));
        list1.add(new Student("bb",90));
        list1.add(new Student("cc",22));
        Collections.sort(list1, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()-o2.getAge();//按照年龄升序
            }
        });
        Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
        System.out.println(list1);
    }
}

以下是一个简单的游戏:

牌类:

java 复制代码
public class Card {
    private String number;
    private String color;
    private int size;

    public Card() {
    }

    public Card(String number, String color, int size) {
        this.number = number;
        this.color = color;
        this.size = size;//牌的大小
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "Card{" +
                "number='" + number + '\'' +
                ", color='" + color + '\'' +
                ", size=" + size +
                '}';
    }
}

操作牌类

java 复制代码
public class Room {
    private ArrayList<Card>list=new ArrayList<>();

    public Room() {
        String []numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        int size=0;
        String []colors={"方块","梅花","红桃","黑桃"};
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                Card c=new Card(number,color,size);
                list.add(c);
            }
        }
        Card c1=new Card("","小王",size++);
        Card c2=new Card("","大王",size);
        Collections.addAll(list,c1,c2);
        System.out.println(list);
    }
    public void randomCard()
    {
        System.out.println("开始洗牌");
        Collections.shuffle(list);
        //System.out.println(list);
    }
    public void devoteCard()
    {
        ArrayList<Card>list1=new ArrayList<>();
        ArrayList<Card>list2=new ArrayList<>();
        ArrayList<Card>list3=new ArrayList<>();

        for (int i = 0; i < list.size()-3; i++) {//只发51张牌
            Card c=list.get(i);
            if(i%3==0)
            {
                list1.add(c);
            }
            else if(i%3==1)
            {
                list2.add(c);
            }
            else
            {
                list3.add(c);
            }
        }
        sortCard(list1);
        sortCard(list2);//对牌进行大小排序
        sortCard(list3);

        System.out.println(list1);
        System.out.println(list2);
        System.out.println(list3);
        System.out.println("底牌是");
        List<Card> LastCard= list.subList(list.size()-3,list.size());//包前不包后
        System.out.println(LastCard);
    }

    private void sortCard(ArrayList<Card> list2) {
        /*Collections.sort(list2, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()-o2.getSize();
            }
        });
    }*/
        Collections.sort(list2, (o1, o2) -> o1.getSize() - o2.getSize());
    }
}

test:

java 复制代码
public class test {
    public static void main(String[] args) {
        Room r=new Room();
        r.randomCard();
        ArrayList<Card>[]lists=new ArrayList[3];
        r.devoteCard();

    }
}

结果:

[Card{number='3', color='方块', size=1}, Card{number='3', color='梅花', size=1}, Card{number='3', color='红桃', size=1}, Card{number='3', color='黑桃', size=1}, Card{number='4', color='方块', size=2}, Card{number='4', color='梅花', size=2}, Card{number='4', color='红桃', size=2}, Card{number='4', color='黑桃', size=2}, Card{number='5', color='方块', size=3}, Card{number='5', color='梅花', size=3}, Card{number='5', color='红桃', size=3}, Card{number='5', color='黑桃', size=3}, Card{number='6', color='方块', size=4}, Card{number='6', color='梅花', size=4}, Card{number='6', color='红桃', size=4}, Card{number='6', color='黑桃', size=4}, Card{number='7', color='方块', size=5}, Card{number='7', color='梅花', size=5}, Card{number='7', color='红桃', size=5}, Card{number='7', color='黑桃', size=5}, Card{number='8', color='方块', size=6}, Card{number='8', color='梅花', size=6}, Card{number='8', color='红桃', size=6}, Card{number='8', color='黑桃', size=6}, Card{number='9', color='方块', size=7}, Card{number='9', color='梅花', size=7}, Card{number='9', color='红桃', size=7}, Card{number='9', color='黑桃', size=7}, Card{number='10', color='方块', size=8}, Card{number='10', color='梅花', size=8}, Card{number='10', color='红桃', size=8}, Card{number='10', color='黑桃', size=8}, Card{number='J', color='方块', size=9}, Card{number='J', color='梅花', size=9}, Card{number='J', color='红桃', size=9}, Card{number='J', color='黑桃', size=9}, Card{number='Q', color='方块', size=10}, Card{number='Q', color='梅花', size=10}, Card{number='Q', color='红桃', size=10}, Card{number='Q', color='黑桃', size=10}, Card{number='K', color='方块', size=11}, Card{number='K', color='梅花', size=11}, Card{number='K', color='红桃', size=11}, Card{number='K', color='黑桃', size=11}, Card{number='A', color='方块', size=12}, Card{number='A', color='梅花', size=12}, Card{number='A', color='红桃', size=12}, Card{number='A', color='黑桃', size=12}, Card{number='2', color='方块', size=13}, Card{number='2', color='梅花', size=13}, Card{number='2', color='红桃', size=13}, Card{number='2', color='黑桃', size=13}, Card{number='', color='小王', size=13}, Card{number='', color='大王', size=14}]

开始洗牌

[Card{number='3', color='方块', size=1}, Card{number='3', color='黑桃', size=1}, Card{number='4', color='黑桃', size=2}, Card{number='4', color='梅花', size=2}, Card{number='4', color='方块', size=2}, Card{number='6', color='方块', size=4}, Card{number='8', color='梅花', size=6}, Card{number='8', color='黑桃', size=6}, Card{number='9', color='梅花', size=7}, Card{number='10', color='黑桃', size=8}, Card{number='10', color='方块', size=8}, Card{number='J', color='红桃', size=9}, Card{number='J', color='梅花', size=9}, Card{number='J', color='黑桃', size=9}, Card{number='K', color='红桃', size=11}, Card{number='A', color='黑桃', size=12}, Card{number='2', color='黑桃', size=13}]

[Card{number='3', color='红桃', size=1}, Card{number='6', color='黑桃', size=4}, Card{number='6', color='红桃', size=4}, Card{number='7', color='方块', size=5}, Card{number='7', color='红桃', size=5}, Card{number='7', color='黑桃', size=5}, Card{number='10', color='梅花', size=8}, Card{number='J', color='方块', size=9}, Card{number='Q', color='黑桃', size=10}, Card{number='K', color='方块', size=11}, Card{number='K', color='梅花', size=11}, Card{number='K', color='黑桃', size=11}, Card{number='A', color='红桃', size=12}, Card{number='2', color='梅花', size=13}, Card{number='2', color='红桃', size=13}, Card{number='', color='小王', size=13}, Card{number='', color='大王', size=14}]

[Card{number='3', color='梅花', size=1}, Card{number='4', color='红桃', size=2}, Card{number='5', color='红桃', size=3}, Card{number='5', color='梅花', size=3}, Card{number='5', color='方块', size=3}, Card{number='5', color='黑桃', size=3}, Card{number='7', color='梅花', size=5}, Card{number='8', color='红桃', size=6}, Card{number='8', color='方块', size=6}, Card{number='9', color='黑桃', size=7}, Card{number='9', color='方块', size=7}, Card{number='9', color='红桃', size=7}, Card{number='10', color='红桃', size=8}, Card{number='Q', color='红桃', size=10}, Card{number='Q', color='梅花', size=10}, Card{number='Q', color='方块', size=10}, Card{number='2', color='方块', size=13}]

底牌是

[Card{number='A', color='梅花', size=12}, Card{number='6', color='梅花', size=4}, Card{number='A', color='方块', size=12}]

相关推荐
雷神乐乐2 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|6 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
jjyangyou11 分钟前
物联网核心安全系列——物联网安全需求
物联网·算法·安全·嵌入式·产品经理·硬件·产品设计
逊嘘26 分钟前
【Java语言】抽象类与接口
java·开发语言·jvm
van叶~28 分钟前
算法妙妙屋-------1.递归的深邃回响:二叉树的奇妙剪枝
c++·算法
简简单单做算法29 分钟前
基于Retinex算法的图像去雾matlab仿真
算法·matlab·图像去雾·retinex
morris13133 分钟前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
云卓SKYDROID43 分钟前
除草机器人算法以及技术详解!
算法·机器人·科普·高科技·云卓科技·算法技术
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word