Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-调用对象数组的整数部分_Java_Arrays_Object - Fatal编程技术网

Java-调用对象数组的整数部分

Java-调用对象数组的整数部分,java,arrays,object,Java,Arrays,Object,我有一个对象数组,其中数组中的每个对象都有一个整数组件、一个字符串和一个缓冲图像组件。我的代码创建了一副52张卡,每张卡都有一个唯一的整数。我想做一个程序,根据这些整数值查找卡片 例如,如果整数10对应于黑桃王,用户可以输入数字10,程序将找到黑桃王并将其移动到牌组顶部。我如何仅根据其“整数值”查找卡 public class Card_Class { private String suit, face; private int value; public Buffere

我有一个对象数组,其中数组中的每个对象都有一个整数组件、一个字符串和一个缓冲图像组件。我的代码创建了一副52张卡,每张卡都有一个唯一的整数。我想做一个程序,根据这些整数值查找卡片

例如,如果整数10对应于黑桃王,用户可以输入数字10,程序将找到黑桃王并将其移动到牌组顶部。我如何仅根据其“整数值”查找卡

public class Card_Class {
    private String suit, face;
    private int value;
    public BufferedImage cardImage;

public Card_Class(String suit, String face, int value, BufferedImage card) {
    this.suit = suit;
    this.face = face;
    this.value = value;
    cardImage = card;

}

public static void main(String[] args) throws IOException {

    Card_Class HeartKing = new Card_Class("Hearts", "King", 13, ImageIO.read(new File("HeartKing.jpg")));
    }
}
下面是甲板建造师:

public class Deck {

public static Card_Class[] deckOfCards;
private int currentCard;

public Deck() throws IOException {

    String[] faces = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
    deckOfCards = new Card_Class[52];
    currentCard = 0;

    final int width = 88;
    final int height = 133;
    final int rows =4;
    final int columns = 13;

    BufferedImage bigImage = ImageIO.read(new File("AllCards.png"));
    BufferedImage tempCardImage;

    for(int suit=0;suit <4; suit++){

        for(int face=0;face<13;face++){
            tempCardImage = bigImage.getSubimage(
                    face*width+(face*10)+8,
                    suit*height+(suit*10)+38,
                    width,
                    height);
            deckOfCards[(face+(suit*13))] = new Card_Class(suits[suit],faces[face],(13*suit+face),tempCardImage);
        }
    }     
   }
公共类甲板{
公共静态卡(U类)【】卡;
个人信用卡;
public Deck()引发异常{
String[]faces={“Ace”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”、“杰克”、“女王”、“国王”};
字符串[]套装={“钻石”、“梅花”、“红心”、“黑桃”};
deckOfCards=新卡类[52];
当前卡=0;
最终整数宽度=88;
最终内部高度=133;
最终整数行=4;
最终整数列=13;
BuffereImage bigImage=ImageIO.read(新文件(“AllCards.png”);
BuffereImage tempCardImage;

对于(int suit=0;suit您可以执行以下操作:

 private Card_Class find(int number){
    Predicate<Card_Class> p1 = c -> c.value == number;
    return Arrays.stream(deckOfCards).anyMatch(p1);
 }
private Card\u类查找(整数){
谓词p1=c->c.value==number;
返回数组.stream(deckOfCards).anyMatch(p1);
}

您的谓词是匹配条件,在本例中是与给定数字匹配的值。

您的问题是什么?如何根据卡片的整数值调用卡片?您可以将deckOfcard从数组更改为HashMap,其中卡片#是键值。@JuanCarlosMendoza我认为这不起作用,因为数组的每个索引这是一个对象。此外,整数值为12的卡片在洗牌后与数组中的第12个条目不对应。我不熟悉谓词。我将做一些研究,看看这是否可以解决我的问题。谓词是在Java 8中引入的,应该适合您;)谢谢@Stefan!你知道在GUI中,如果用户单击一张卡的图像,程序就会找到该卡的整数值,那么这是否会起作用吗?这取决于你的实现。单击该卡时,你有哪些数据?我还没有为其编写代码,但计划重新制作一个用户单击的纸牌游戏版本卡和程序将查看该卡的整数值,并确定该值是否比第二张卡的整数值小一。