App下載

Java實戰(zhàn)練習(xí) 實現(xiàn)一個簡單的撲克牌魔術(shù)

貧困大賽形象代言人 2021-08-07 14:17:15 瀏覽數(shù) (2336)
反饋

有很多魔術(shù)師都標演過撲克牌魔術(shù),外行看著熱鬧,覺得神奇,但若是細究的話,都可以發(fā)現(xiàn)其中的規(guī)律。下面,將通過這個規(guī)律,用 Java 來寫一個簡單的撲克牌魔術(shù)!

前言

用到了Scanner,for循環(huán),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<>();
        //直接定義兩個數(shù)組并賦值,一個存四種花色,一個存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("小王");
        //組裝兩個集合的數(shù)據(jù),生成帶花色的52張牌(可以用普通for循環(huán),也可以用增強for循環(huán)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("請選擇任意一張撲克牌,并記住它?!据斎胄懽帜?#39;y'繼續(xù),輸入其他任意字符結(jié)束游戲!】");
        Scanner scanner1 = new Scanner(System.in);

        if (scanner1.nextLine().equals("y")){
            System.out.println("系統(tǒng)將這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疊請輸入數(shù)字1;如果在第2疊請輸入數(shù)字2;如果在第3疊請輸入數(shù)字3");
        System.out.println("請輸入:");
        //新建3個數(shù)組,分3次重新存重新按指定規(guī)則排序的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疊請輸入數(shù)字1;如果在第2疊請輸入數(shù)字2;如果在第3疊請輸入數(shù)字3");
        System.out.println("請輸入:");
        int b = scanner3.nextInt();
        //新建3個數(shù)組,分3次重新存重新按指定規(guī)則排序的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疊請輸入數(shù)字1;如果在第2疊請輸入數(shù)字2;如果在第3疊請輸入數(shù)字3");
        System.out.println("請輸入:");

        //新建3個數(shù)組,分3次重新存重新按指定規(guī)則排序的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("什么?你說我是運氣好?不!這是“魔法”!");
    }
}

運行結(jié)果

其中一次運行結(jié)果:開始我記住的就是梅花K!

魔術(shù)表演步驟

值得一提的是,將21張撲克牌分成3組的具體操作,應(yīng)該是:

將21張撲克牌拿在手上,按順序,從左到右依次發(fā)牌,每發(fā)3次算一輪,一共發(fā)七輪,發(fā)完就會產(chǎn)生3疊牌,每疊7張!

到此這篇關(guān)于用Java來實現(xiàn)一個簡單的撲克牌魔術(shù)的文章就介紹到這了,想要了解更多相關(guān)Java有趣好玩的實戰(zhàn)內(nèi)容嗎,請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家多多支持!

0 人點贊