当前位置:   article > 正文

java中可变参数和简单游戏

java中可变参数和简单游戏

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

可变参数的好处:
灵活的接收数据

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

可变参数的注意事项:

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

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

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

  1. public class test {
  2. public static void main(String[] args) {
  3. ttt();//可以不传数据
  4. ttt(1);//可以传一个数据
  5. ttt(1,2,3);//可以传多个数据
  6. ttt(new int[]{1,2,3});//可以传一个数组
  7. }
  8. public static void ttt(int...nums)
  9. {
  10. //可变参数在方法内部是一个数组
  11. System.out.println(Arrays.toString(nums));
  12. }
  13. //只能有一个可变参数,且必须在形参列表的最后
  14. public static void ppp(int age,int...s)
  15. {}
  16. }

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集合中的元素,按照比较器对象指定的规则进行排序 

  1. public class test {
  2. public static void main(String[] args) {
  3. //1 addAll
  4. ArrayList<String>list=new ArrayList<>();
  5. Collections.addAll(list,"java1","java2","java3");
  6. System.out.println(list);
  7. //2:shuffle
  8. Collections.shuffle(list);
  9. System.out.println(list);//[java2, java3, java1]
  10. //sort,默认升序
  11. Collections.sort(list);
  12. System.out.println(list);//[java1, java2, java3]
  13. //sort排序自定义对象
  14. ArrayList<Student>list1=new ArrayList<>();
  15. list1.add(new Student("aa",10));
  16. list1.add(new Student("bb",90));
  17. list1.add(new Student("cc",22));
  18. Collections.sort(list1, new Comparator<Student>() {
  19. @Override
  20. public int compare(Student o1, Student o2) {
  21. return o1.getAge()-o2.getAge();//按照年龄升序
  22. }
  23. });
  24. Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
  25. System.out.println(list1);
  26. }
  27. }public class test {
  28. public static void main(String[] args) {
  29. //1 addAll
  30. ArrayList<String>list=new ArrayList<>();
  31. Collections.addAll(list,"java1","java2","java3");
  32. System.out.println(list);
  33. //2:shuffle
  34. Collections.shuffle(list);
  35. System.out.println(list);//[java2, java3, java1]
  36. //sort,默认升序
  37. Collections.sort(list);
  38. System.out.println(list);//[java1, java2, java3]
  39. //sort排序自定义对象
  40. ArrayList<Student>list1=new ArrayList<>();
  41. list1.add(new Student("aa",10));
  42. list1.add(new Student("bb",90));
  43. list1.add(new Student("cc",22));
  44. Collections.sort(list1, new Comparator<Student>() {
  45. @Override
  46. public int compare(Student o1, Student o2) {
  47. return o1.getAge()-o2.getAge();//按照年龄升序
  48. }
  49. });
  50. Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
  51. System.out.println(list1);
  52. }
  53. }

以下是一个简单的游戏:
牌类:

  1. public class Card {
  2. private String number;
  3. private String color;
  4. private int size;
  5. public Card() {
  6. }
  7. public Card(String number, String color, int size) {
  8. this.number = number;
  9. this.color = color;
  10. this.size = size;//牌的大小
  11. }
  12. public String getNumber() {
  13. return number;
  14. }
  15. public void setNumber(String number) {
  16. this.number = number;
  17. }
  18. public String getColor() {
  19. return color;
  20. }
  21. public void setColor(String color) {
  22. this.color = color;
  23. }
  24. public int getSize() {
  25. return size;
  26. }
  27. public void setSize(int size) {
  28. this.size = size;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Card{" +
  33. "number='" + number + '\'' +
  34. ", color='" + color + '\'' +
  35. ", size=" + size +
  36. '}';
  37. }
  38. }

 操作牌类

  1. public class Room {
  2. private ArrayList<Card>list=new ArrayList<>();
  3. public Room() {
  4. String []numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
  5. int size=0;
  6. String []colors={"方块","梅花","红桃","黑桃"};
  7. for (String number : numbers) {
  8. size++;
  9. for (String color : colors) {
  10. Card c=new Card(number,color,size);
  11. list.add(c);
  12. }
  13. }
  14. Card c1=new Card("","小王",size++);
  15. Card c2=new Card("","大王",size);
  16. Collections.addAll(list,c1,c2);
  17. System.out.println(list);
  18. }
  19. public void randomCard()
  20. {
  21. System.out.println("开始洗牌");
  22. Collections.shuffle(list);
  23. //System.out.println(list);
  24. }
  25. public void devoteCard()
  26. {
  27. ArrayList<Card>list1=new ArrayList<>();
  28. ArrayList<Card>list2=new ArrayList<>();
  29. ArrayList<Card>list3=new ArrayList<>();
  30. for (int i = 0; i < list.size()-3; i++) {//只发51张牌
  31. Card c=list.get(i);
  32. if(i%3==0)
  33. {
  34. list1.add(c);
  35. }
  36. else if(i%3==1)
  37. {
  38. list2.add(c);
  39. }
  40. else
  41. {
  42. list3.add(c);
  43. }
  44. }
  45. sortCard(list1);
  46. sortCard(list2);//对牌进行大小排序
  47. sortCard(list3);
  48. System.out.println(list1);
  49. System.out.println(list2);
  50. System.out.println(list3);
  51. System.out.println("底牌是");
  52. List<Card> LastCard= list.subList(list.size()-3,list.size());//包前不包后
  53. System.out.println(LastCard);
  54. }
  55. private void sortCard(ArrayList<Card> list2) {
  56. /*Collections.sort(list2, new Comparator<Card>() {
  57. @Override
  58. public int compare(Card o1, Card o2) {
  59. return o1.getSize()-o2.getSize();
  60. }
  61. });
  62. }*/
  63. Collections.sort(list2, (o1, o2) -> o1.getSize() - o2.getSize());
  64. }
  65. }

test:

  1. public class test {
  2. public static void main(String[] args) {
  3. Room r=new Room();
  4. r.randomCard();
  5. ArrayList<Card>[]lists=new ArrayList[3];
  6. r.devoteCard();
  7. }
  8. }

 

 结果:

[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}]
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/441805
推荐阅读
相关标签
  

闽ICP备14008679号