这篇文章将为大家详细讲解有关Java如何实现一个简单的扑克牌魔术,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
用到了Scanner,for循环,if-else语句,集合,线程的Thread.sleep()方法
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CardMagic {
public static void main(String[] args) {
//1.准备54张牌
//新建一个集合存储54张牌并打乱顺序
ArrayList<String> pokers = new ArrayList<>();
//直接定义两个数组并赋值,一个存四种花色,一个存13张牌的值(3,4,5,6,7,8,9,10,J,Q,K,A,2)
String[] kind = {"♥","♠","♦","♣"};
String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//先存大王小王到pokers集合中
pokers.add("大王");
pokers.add("小王");
//组装两个集合的数据,生成带花色的52张牌(可以用普通for循环,也可以用增强for循环foreach)
for (int i = 0; i < kind.length; i+ +) {
for (int j = 0; j < 13; j++) {
pokers.add(kind[i]+number[j]);
}
}
System.out.println("这里有54张牌:"+pokers);//顺序打印
//洗牌--打乱pokers集合里的元素顺序
Collections.shuffle(pokers);//打乱顺序
//再新建一个集合,用来存储随机拿出的21张牌(伪随机:其实是拿出前21张牌,但是pokers集合是随机打乱顺序的,所以每次运行程序,拿出的前21张牌也就不一样且乱序了)
ArrayList<String> list21 = new ArrayList<>();
for (int i = 0; i < 21; i++) {
list21.add(pokers.get(i));
}
//打印出这21张牌
System.out.println("随机取出21张扑克牌并打乱顺序:");
System.out.println(list21);
System.out.println("请选择任意一张扑克牌,并记住它。【输入小写字母'y'继续,输入其他任意字符结束游戏!】");
Scanner scanner1 = new Scanner(System.in);
if (scanner1.nextLine().equals("y")){
System.out.println("系统将这21张扑克牌均分为3叠,请等待...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.exit(-1);
}
//定义3个集合,每个集合存储7张牌
ArrayList<String> card1 = new ArrayList<>();
ArrayList<String> card2 = new ArrayList<>();
ArrayList<String> card3 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
card1.add(list21.get(i));
}
for (int i = 7; i < 14; i++) {
card2.add(list21.get(i));
}
for (int i = 14; i < 21; i++) {
card3.add(list21.get(i));
}
System.out.println("第1叠:"+card1);
System.out.println("第2叠:"+card2);
System.out.println("第3叠:"+card3);
//判断输入
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner scanner2 = new Scanner(System.in);
System.out.println("请告诉我你记住的牌在第几叠~如果在第1叠请输入数字1;如果在第2叠请输入数字2;如果在第3叠请输入数字3");
System.out.println("请输入:");
//新建3个数组,分3次重新存重新按指定规则排序的21张牌
ArrayList<String> list21New1 = new ArrayList<>();
int a = scanner2.nextInt();
if (a==1){
for (int i = 0; i < 7; i++) {
list21New1.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card3.get(i));
}
}else if (a==2){
for (int i = 0; i < 7; i++) {
list21New1.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card3.get(i));
}
}else {
for (int i = 0; i < 7; i++) {
list21New1.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card3.get(i));
}
for (int i = 0; i < 7; i++) {
list21New1.add(card2.get(i));
}
}
card1.clear();
card2.clear();
card3.clear();
for (int i = 0; i < 21; i++) {
card1.add(list21New1.get(i));
i++;
card2.add(list21New1.get(i));
i++;
card3.add(list21New1.get(i));
}
System.out.println("第1叠:"+card1);
System.out.println("第2叠:"+card2);
System.out.println("第3叠:"+card3);
//判断输入
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner scanner3 = new Scanner(System.in);
System.out.println("请告诉我你记住的牌在第几叠~如果在第1叠请输入数字1;如果在第2叠请输入数字2;如果在第3叠请输入数字3");
System.out.println("请输入:");
int b = scanner3.nextInt();
//新建3个数组,分3次重新存重新按指定规则排序的21张牌
ArrayList<String> list21New2 = new ArrayList<>();
if (b==1){
for (int i = 0; i < 7; i++) {
list21New2.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card3.get(i));
}
}else if (b==2){
for (int i = 0; i < 7; i++) {
list21New2.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card3.get(i));
}
}else {
for (int i = 0; i < 7; i++) {
list21New2.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card3.get(i));
}
for (int i = 0; i < 7; i++) {
list21New2.add(card2.get(i));
}
}
card1.clear();
card2.clear();
card3.clear();
for (int i = 0; i < 21; i++) {
card1.add(list21New2.get(i));
i++;
card2.add(list21New2.get(i));
i++;
card3.add(list21New2.get(i));
}
System.out.println("第1叠:"+card1);
System.out.println("第2叠:"+card2);
System.out.println("第3叠:"+card3);
//判断输入
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner scanner4 = new Scanner(System.in);
System.out.println("请告诉我你记住的牌在第几叠~如果在第1叠请输入数字1;如果在第2叠请输入数字2;如果在第3叠请输入数字3");
System.out.println("请输入:");
//新建3个数组,分3次重新存重新按指定规则排序的21张牌
ArrayList<String> list21New3 = new ArrayList<>();
int c = scanner2.nextInt();
if (c==1){
for (int i = 0; i < 7; i++) {
list21New3.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card3.get(i));
}
}else if (c==2){
for (int i = 0; i < 7; i++) {
list21New3.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card2.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card3.get(i));
}
}else {
for (int i = 0; i < 7; i++) {
list21New3.add(card1.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card3.get(i));
}
for (int i = 0; i < 7; i++) {
list21New3.add(card2.get(i));
}
}
System.out.println("好了,稍等片刻!让我猜猜你记住的是什么牌......");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("真相永远只有一个,你选的牌是"+list21New3.get(10)+"!");
System.out.println("什么?你说我是运气好?不!这是“魔法”!");
}
}
关于“Java如何实现一个简单的扑克牌魔术”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。