52张牌的甲板,1到ace,3个随机弹出,如何为这些卡添加值? king = 10 ace = 1 [英] Deck of 52 cards, 1 through ace, three pop up randomly, how do I add values to these cards? king = 10 ace = 1

查看:63
本文介绍了52张牌的甲板,1到ace,3个随机弹出,如何为这些卡添加值? king = 10 ace = 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import javax.swing.*;

public class Cardvisible extends JFrame {
  private static final long serialVersionUID = 1L;

  public Cardvisible() {
    int[] list = new int[54];
    for (int i = 0; i < list.length; i++)
      list[i] = i + 1;
    shuffle(list);

    setLayout(new GridLayout(1, 3));
    add(new JLabel(new ImageIcon("image/card/" + list[0] + ".png")));
    add(new JLabel(new ImageIcon("image/card/" + list[1] + ".png")));
    add(new JLabel(new ImageIcon("image/card/" + list[2] + ".png")));
  }

  public static void shuffle(int[] list) {
    for (int i = 0; i < list.length; i++) {
      // Generate an index randomly
      int index = (int)(Math.random() * list.length);

      // Swap myList[i] with myList[index]
      int temp =list[i];
      list[i] = list[index];
      list[index] = temp;
    }
  }

  public static void main(String[] args) {
    Cardvisible frame = new Cardvisible();
    frame.setTitle("Exercise12_09");
    frame.setSize(300, 170);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);
  }
}

推荐答案

创建 class并赋予它属性,包括suit和faceValue,如:

Create a Card class and give it properties, including suit and faceValue, like:
class Card {
    enum suit {
        Spades, Hearts, Diamonds, Clubs
    };

    private suit cardSuit;
    private int faceValue;

    public Card(suit theSuit, int theValue); // constructor

    // add methods, getters and setters here ...


这篇关于52张牌的甲板,1到ace,3个随机弹出,如何为这些卡添加值? king = 10 ace = 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆