题目:
方法一:
package com.qfstu.javase.Week03.Day01;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class HomeWork03_1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
list.add("?" + i);
}
for (int i = 1; i <= 13; i++) {
list.add("?" + i);
}
for (int i = 1; i <= 13; i++) {
list.add("?" + i);
}
for (int i = 1; i <= 13; i++) {
list.add("?" + i);
}
Random random = new Random();
for(int i = 1; i <= 52; i++) {
int a = random.nextInt(list.size());
int b = random.nextInt(list.size());
String poker1 = list.get(a);
String poker2 = list.get(b);
list.set(a, poker2);
list.set(b, poker1);
}
int i = 0;
for (String s : list) {
i++;
System.out.print(s + "\t");
if(i % 13 == 0) {
System.out.println();
}
}
}
}
方法二:
package com.qfstu.javase.Week03.Day01;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class HomeWork03_2 {
public static void main(String[] args) {
List<Poker> list = new ArrayList<>();
String[] str1 = {"?","?","?","?"};
String[] str2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < str1.length; i++) {
for (int j = 0; j < str2.length; j++) {
list.add(new Poker(str1[i],str2[j]));
}
}
Random random = new Random();
for(int i = 1; i <= 52; i++) {
int a = random.nextInt(list.size());
int b = random.nextInt(list.size());
Poker poker1 = list.get(a);
Poker poker2 = list.get(b);
list.set(a, poker2);
list.set(b, poker1);
}
int i = 0;
for (Poker poker : list) {
System.out.print(poker + "\t");
i++;
if(i % (list.size()/4) == 0) {
System.out.println();
}
}
}
}
class Poker{
private String color;
private String num;
@Override
public String toString() {
return getColor() + getNum();
}
public Poker() {
}
public Poker(String color, String num) {
this.color = color;
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
|