欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实战练习之扑克牌魔术

程序员文章站 2022-03-25 10:51:04
前言用到了scanner,for循环,if-else语句,集合,线程的thread.sleep()方法话不多说,直接上代码代码import java.util.arraylist;import jav...

前言

用到了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!

Java实战练习之扑克牌魔术

魔术表演步骤

Java实战练习之扑克牌魔术

值得一提的是,将21张扑克牌分成3组的具体操作,应该是:

将21张扑克牌拿在手上,按顺序,从左到右依次发牌,每发3次算一轮,一共发七轮,发完就会产生3叠牌,每叠7张!

到此这篇关于java实战练习之扑克牌魔术的文章就介绍到这了,更多相关java扑克牌魔术内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!