Java斗地主项目碎片

1.从52张扑克牌中随机抽5张牌

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Ankiia
 */
import java.util.*;
public class Main {
    public static void main(String[] args){
        Random r=new Random();
        int a[]=new int[53];
        int b[]=new int[5];
        int k=0;
        for(int i=0;i<5;i++){
            k=1+r.nextInt(52);
            if(a[k]==1){
                i--;
                continue;
            }
            else{
                b[i]=k;
                a[k]=1;
            }
        }
        for(int i=0;i<5;i++){
            if(b[i]>=1 && b[i]<=13){
                System.out.print("梅花");
            }else if(b[i]>=14 && b[i]<=26){
                System.out.print("方块");
            }else if(b[i]>=27 && b[i]<=39){
                System.out.print("黑桃");
            }else if(b[i]>=40 && b[i]<=52){
                System.out.print("红心");
            }
            System.out.print(trans(b[i]%13)+"\t");
        }
    }
    public static char trans(int k){
        switch (k){
            case 10:
                System.out.print("10");
                return 0;
            case 1:
                return 'A';
            case 11:
                return 'J';
            case 12:
                return 'Q';
            case 0:
                return 'K';
            default:
                return (char)(k+48);
        }
    }
}

posted @ 2024-11-05 08:51  Ankiia  阅读(46)  评论(1)    收藏  举报