java扑克牌魔术 Java实战练习之扑克牌魔术
牛牛ō^ō 人气:0想了解Java实战练习之扑克牌魔术的相关内容吗,牛牛ō^ō在本文为您仔细讲解java扑克牌魔术的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java扑克牌魔术,java魔术,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("什么?你说我是运气好?不!这是“魔法”!"); } }
运行结果
其中一次运行结果:开始我记住的就是梅花K!
魔术表演步骤
值得一提的是,将21张扑克牌分成3组的具体操作,应该是:
将21张扑克牌拿在手上,按顺序,从左到右依次发牌,每发3次算一轮,一共发七轮,发完就会产生3叠牌,每叠7张!
加载全部内容