对象是否可以与其类型的其他对象进行交互

时间:2016-01-28 04:46:43

标签: java class object

我对java比较陌生,我正试图为我的二十一点游戏制作卡片类。我想知道是否有办法让它知道已经创建的其他Card对象来检查他们的卡值并确保我不创建重复。任何其他改进我的代码的技巧也非常感谢。

class Card{
private int cardNum;
//1-4 ace 5-8 two 9-12 three 13-16 four 17-20 five 21-24 six 25-28 seven 29-32 eight 33-36 nine 
//37-40 ten 41-44 jack 45-48 queen 49-52 king
private int value;

public String toString(){
    return findCard() + " " + findSuit();
}

public Card(){
    cardNum = (int)(Math.random * 52)+1;
    giveValue();

}

public String findSuit(){
    if(cardNum % 4 == 1){
        return "of clubs";
    } else if(cardNum % 4 == 2){
        return "of diamonds";
    } else if(cardNum % 4 == 3){
        return "of hearts";
    } else {
        return "of spades";
    }
}
public void giveValue(){
    if(cardNum % 13 == 1){
        value = 11;
    } else if(cardNum % 13 == 2){
        value = 2;
    } else if(cardNum % 13 == 3){
        value = 3;
    } else if(cardNum % 13 == 4){
        value = 4;
    } else if(cardNum % 13 == 5){
        value = 5;
    } else if(cardNum % 13 == 6){
        value = 6;
    } else if(cardNum % 13 == 7){
        value = 7;
    } else if(cardNum % 13 == 8){
        value = 8;
    } else if(cardNum % 13 == 9){
        value = 9;
    } else if(cardNum % 13 == 10){
        value = 10;
    } else if(cardNum % 13 == 11){
        value = 10;
    } else if(cardNum % 13 == 12){
        value = 10;
    } else {
        value = 10;
    }
}
public String findCard(){
    if(value == 11){
        return "ace";
    } else if(value == 2){
        return "two";
    } else if(value == 3){
        return "three";
    } else if(value == 4){
        return "four";
    } else if(value == 5){
        return "five";
    } else if(value == 6){
        return "six";
    } else if(value == 7){
        return "seven";
    } else if(value == 8){
        return "eight";
    } else if(value == 9){
        return "nine";
    } else if(cardNum % 13 == 10){
        return "ten";
    } else if(cardNum % 13 == 11{
        return "jack";
    } else if(cardNum % 13 == 12{
        return "queen";
    } else {
        reutrn "king";
    }
}

0 个答案:

没有答案