本文实例讲述了Java模拟扑克牌洗牌实现生成52张扑克的方法。分享给大家供大家参考,具体如下:
要求:
生成52张扑克,模拟扑克牌洗牌,并输出。
实现代码:
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
|
package com.NCU.ZHANGhuirong;
import java.util.ArrayList;
import java.util.Collections;
public class Card {
public String poker( int num) {
String str = "" ;
String[] face = { "♥" , "♠" , "♣" , "♦" };
String[] number = { "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ,
"J" , "Q" , "K" };
str += face[num % 4 ] + number[num % 13 ];
return str;
}
public void shuffle( @SuppressWarnings ( "rawtypes" ) ArrayList list) {
}
@SuppressWarnings ( "unchecked" )
public static void main(String[] args) {
@SuppressWarnings ( "rawtypes" )
ArrayList list = new ArrayList();
Card card = new Card();
for ( int i = 0 ; i < 52 ; i++) {
list.add(card.poker(i));
}
for ( int i = 0 ; i < list.size(); i++) {
System.out.printf( "%s\t" , list.get(i));
if ((i + 1 ) % 13 == 0 ) {
System.out.println( " " );
}
}
System.out.println();
Collections.shuffle(list);
System.out.println( "洗牌后:" );
for ( int i = 0 ; i < list.size(); i++) {
System.out.printf( "%s\t" , list.get(i));
if ((i + 1 ) % 13 == 0 ) {
System.out.println( " " );
}
}
}
}
|
输出:
1
2
3
4
5
6
7
8
9
10
|
♥A ♠ 2 ♣ 3 ♦ 4 ♥ 5 ♠ 6 ♣ 7 ♦ 8 ♥ 9 ♠ 10 ♣J ♦Q ♥K
♠A ♣ 2 ♦ 3 ♥ 4 ♠ 5 ♣ 6 ♦ 7 ♥ 8 ♠ 9 ♣ 10 ♦J ♥Q ♠K
♣A ♦ 2 ♥ 3 ♠ 4 ♣ 5 ♦ 6 ♥ 7 ♠ 8 ♣ 9 ♦ 10 ♥J ♠Q ♣K
♦A ♥ 2 ♠ 3 ♣ 4 ♦ 5 ♥ 6 ♠ 7 ♣ 8 ♦ 9 ♥ 10 ♠J ♣Q ♦K
洗牌后:
♥ 3 ♥ 9 ♦ 6 ♥J ♦K ♥ 4 ♦ 8 ♥K ♦Q ♦ 5 ♣ 7 ♠J ♠A
♦ 10 ♣A ♥ 8 ♠ 9 ♥Q ♦ 4 ♠ 6 ♠ 8 ♥ 10 ♣ 2 ♣ 10 ♦ 7 ♠ 10
♥A ♣J ♠K ♠ 5 ♥ 2 ♣ 8 ♦J ♠Q ♦ 3 ♦ 9 ♣Q ♣K ♣ 3
♥ 5 ♣ 6 ♣ 5 ♦ 2 ♦A ♥ 7 ♠ 4 ♥ 6 ♠ 7 ♣ 4 ♠ 3 ♠ 2 ♣ 9
|
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/qq_37999723/article/details/78348249