Java基础高级综合练习题扑克牌的创建

2025-05-29 0 41

最近学了很多的知识,脑容量小,记不清,还是得做做练习!

今天就做了一个扑克牌的练习

首先呢..这个逻辑一定要非常清楚,我们要想做出一副扑克牌,必定要弄清楚每一张牌和整的一副牌

Java基础高级综合练习题扑克牌的创建

首先分析 一张扑克

一张牌里面有什么?相信大家看图(图不是我写的)就应该懂了,一张扑克有属于它自己的花色(红桃,黑桃,梅花,方块) 以及自己的点数(a,2,3…..j,q,k)就这两种属性,对吧!

那么花色符号,点数符号是个啥? 花色符号就是来代替我们的花色的,我们不可能拿着“红桃”这种文字写进程序吧!所以我们可以用数字来代替

我们就按照下面的,一 一对应

?

1

2

3

4

5

6
/**

* 王 ♥ ♠ ♣ ♦

* 1 2 3 4 5

* a j q k 小王 大王

* 1 11 12 13 14 15

**/

好了,我们已经把每张特殊一点的扑克给对应好了!我们可以开始写代码了

我的代码文件:

  • apoker.java————–一张扑克
  • poker.java—————-一副扑克
  • test.java——————测试

apoker.java先给大家展示

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
public class apoker {

//implements comparable<apoker>

//花色

private int color;

//点数

private int count;

//花色符号

private string colortext;

//点数符号

private string counttext;

//写构造方法

public apoker(int color, int count, string colortext, string counttext) {

super();

this.color = color;

this.count = count;

this.colortext = colortext;

this.counttext = counttext;

}

//get set 方法,进行封装

public int getcolor() {

return color;

}

public void setcolor(int color) {

this.color = color;

}

public int getcount() {

return count;

}

public void setcount(int count) {

this.count = count;

}

public string getcolortext() {

return colortext;

}

public void setcolortext(string colortext) {

this.colortext = colortext;

}

public string getcounttext() {

return counttext;

}

public void setcounttext(string counttext) {

this.counttext = counttext;

}

//重写 tostring 方法,因为我们需要显示每张牌的具体情况

@override

public string tostring() {

return "apoker [color=" + color + ", count=" + count + ", colortext=" + colortext + ", counttext=" + counttext

+ "]\\n";

}

}

这里还是非常容易理解的,无非就是进行了封装和重写tostring方法。

ok,一张扑克写完了,我们接下来写一副扑克牌

一副扑克牌

我再把那个图拿下来

Java基础高级综合练习题扑克牌的创建

我们发现一副扑克牌里面有花色数量,扑克牌的数量,以及所有牌(所有牌也就是一个集合)。另外这里面还有着几个方法,这里我就写创建扑克(),洗牌()抽取一张() 吧。

现在看下

poker.java:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105
import java.util.arraylist;

import java.util.collections;

import java.util.list;

import java.util.random;

public class poker {

//花色数量

private int colorcount;

//牌的数量

private int pokercount;

//牌的集合

private list<apoker> mlist;

//进行封装

public int getcolorcount() {

return colorcount;

}

public void setcolorcount(int colorcount) {

this.colorcount = colorcount;

}

public int getpokercount() {

return pokercount;

}

public void setpokercount(int pokercount) {

this.pokercount = pokercount;

}

public list<apoker> getmlist() {

return mlist;

}

public void setmlist(list<apoker> mlist) {

this.mlist = mlist;

}

/**

* 王 ♥ ♠ ♣ ♦

* 1 2 3 4 5

* a j q k 小王 大王

* 1 11 12 13 14 15

**/

//创建一副扑克牌

public list<apoker> creatpoker() {

//初始化colorcount pokercount

colorcount=5;//一副扑克有 王 ♥ ♠ ♣ ♦这五种花色

pokercount=54;//一副扑克共有54张牌

mlist=new arraylist<apoker>();

// ♥ ♠ ♣ ♦----------先分析这四种,因为这四种里面才含有a-k的值,小王大王后面处理

for (int i = 2; i <=5; i++) {

//得到每种花色里面的牌

for (int j = 1; j <= 13; j++) {

string colortext=null;

string counttext=null;

switch (i) {

case 2:

colortext="♥";

break;

case 3:

colortext="♠";

break;

case 4:

colortext="♣";

break;

case 5:

colortext="♦";

break;

}

switch (j) {

case 1:

counttext="a";

break;

case 11:

counttext="j";

break;

case 12:

counttext="q";

break;

case 13:

counttext="k";

break;

default:

counttext=j+""; //除了a,j,q,k,都直接使用数字,这里是将j转化为字符

break;

}

apoker apoker1=new apoker(i, j, colortext, counttext);

mlist.add(apoker1); //把♥ ♠ ♣ ♦这四种花色塞进一副扑克里面

}

}

apoker apoker2=new apoker(1, 14, "王", "小王");//写小王

apoker apoker3=new apoker(1, 14, "王", "大王");//写大王

mlist.add(apoker2);//把小王塞进一副扑克里面去

mlist.add(apoker3);//把大王塞进一副扑克里面去

return mlist;

}

/**

*洗牌方法

**/

public list<apoker> shufflepoker() {

collections.shuffle(mlist); //这是collections的一个把集合打乱的方法

return mlist;

}

/**

* 随机抽牌

**/

public apoker getrandompoker() {

random random=new random();//获取一个随机数

int index=random.nextint(54);

return mlist.get(index);

}

}

这里慢慢看也很容易的,我已经全部把每一步解释了,大家根据那个对应关系应该很容易理解。

两个写好了,我们可以进行使用了

test.java就是我们用来测试我们之前写好的代码!

创建一副扑克

?

1

2

3

4

5

6

7

8
import java.util.list;

public class test {

public static void main(string[] args) {

poker poker=new poker();//创建一副扑克对象

list<apoker> mlist=poker.creatpoker(); 调用creatpoker()方法,创建一副扑克

system.out.println(mlist);打印出来!

}

}

我们来看结果

Java基础高级综合练习题扑克牌的创建

ok 54张扑克被创建了!

洗牌

我们修改一下test.java的内容

?

1

2

3

4

5

6

7

8

9
import java.util.list;

public class test {

public static void main(string[] args) {

poker poker=new poker();

list<apoker> mlist=poker.creatpoker();

list<apoker> mlist2=poker.shufflepoker();

system.out.println(mlist2);

}

}

打印一下

Java基础高级综合练习题扑克牌的创建

果然,,牌的顺序已经乱了,我们进行了洗牌

随机抽牌

我们继续重写一下test.java

?

1

2

3

4

5

6

7

8

9
import java.util.list;

public class test {

public static void main(string[] args) {

poker poker=new poker();

list<apoker> mlist=poker.creatpoker();

apoker ap=poker.getrandompoker();

system.out.println(ap);

}

}

打印一下

Java基础高级综合练习题扑克牌的创建

果然它随机抽取了一张,每次打印抽取的牌都是不同的,这里就不展示了!

ok,大家继续学习吧,come on!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_36547531/article/details/81582632

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java基础高级综合练习题扑克牌的创建 https://www.kuaiidc.com/109834.html

相关文章

发表评论
暂无评论