Java基础练习——读心术(扑克牌魔术——21张扑克牌)--代码简化版
将重复代码封装成一个方法,达到简化代码的目的
话不多说,直接上代码!😎😎😎
欢迎大佬对代码进行优化,魔术原理请自行百度"21张扑克牌"(本文最后附带表演手法图片)。
代码
import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;public class CardMagic { public static int count = 0;//用来统计重复询问观众的次数,第三次询问之后 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(2000); } 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)); } reorder(1,card1,card2,card3); //判断输入 Scanner scanner2 = new Scanner(System.in); int a = scanner2.nextInt(); reorder(a,card1,card2,card3); //判断输入 Scanner scanner3 = new Scanner(System.in); int b = scanner3.nextInt(); reorder(b,card1,card2,card3); //判断输入 Scanner scanner4 = new Scanner(System.in); int c = scanner4.nextInt(); reorder(c,card1,card2,card3); System.out.println("好了,稍等片刻!让我猜猜你记住的是什么牌......"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("真相永远只有一个,你选的牌是"+card2.get(3)+"!"); System.out.println("什么?你说我是运气好?不!这是"魔法"!"); } public static void reorder(int a,ArrayList<String> card1,ArrayList<String> card2,ArrayList<String> card3){ ArrayList<String> list21New = new ArrayList<>(); count++; if (a==1){ for (int i = 0; i < 7; i++) { list21New.add(card2.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card1.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card3.get(i)); } }else if (a==2){ for (int i = 0; i < 7; i++) { list21New.add(card1.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card2.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card3.get(i)); } }else { for (int i = 0; i < 7; i++) { list21New.add(card1.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card3.get(i)); } for (int i = 0; i < 7; i++) { list21New.add(card2.get(i)); } } card1.clear(); card2.clear(); card3.clear(); for (int i = 0; i < 21; i++) { card1.add(list21New.get(i)); i++; card2.add(list21New.get(i)); i++; card3.add(list21New.get(i)); } if (count<4){ System.out.println("第1叠:"+card1); System.out.println("第2叠:"+card2); System.out.println("第3叠:"+card3); System.out.println("请告诉我你记住的牌在第几叠~如果在第1叠请输入数字1;如果在第2叠请输入数字2;如果在第3叠请输入数字3"); System.out.println("请输入:"); } }}
运行结果
其中一次运行结果:开始我记住的就是梅花K!😲
魔术表演步骤
值得一提的是,将21张扑克牌分成3组的具体操作,应该是:
将21张扑克牌拿在手上,按顺序,从左到右依次发牌,每发3次算一轮,一共发七轮,发完就会产生3叠牌,每叠7张!😎😎😎
欢迎评论区讨论😁😁😁希望多多点赞,哈哈~
原文转载:http://www.shaoqun.com/a/710756.html
marks spencer:https://www.ikjzd.com/w/2385
shirley:https://www.ikjzd.com/w/1684
Java基础练习——读心术(扑克牌魔术——21张扑克牌)--代码简化版将重复代码封装成一个方法,达到简化代码的目的话不多说,直接上代码!😎😎😎欢迎大佬对代码进行优化,魔术原理请自行百度"21张扑克牌"(本文最后附带表演手法图片)。代码importjava.util.ArrayList;importjava.util.Collections;importjava.util.Scanner;pu
一淘比价网:https://www.ikjzd.com/w/1698
c88:https://www.ikjzd.com/w/1017.html
淘粉吧怎么返利:https://www.ikjzd.com/w/1725
口述:老公屡约前妻玩暧昧该不该离婚:http://www.30bags.com/m/a/254282.html
外贸课堂:64个国家风险等级及主要风险分析!:https://www.ikjzd.com/home/108300
Shopee开店会涉及到哪些隐形成本?:https://www.ikjzd.com/home/111402
No comments:
Post a Comment