如何在不创建数组的情况下检查重复值?

时间:2015-10-28 14:39:21

标签: java

我有一个程序可以根据随机数的值处理20张卡片。到目前为止,它可以处理诸如黑桃之王,2心之类等卡片。我的工作是使用方法检查卡片是否重复,但没有阵列。这是我检查重复项的解决方案,这些重复项由于显而易见的原因而无效:

    public class Driver {
        public static void main(String [] args) {

            for (int i = 0; i < 20; i++){
                Cards card1 = new Cards();
                Cards card2 = card1;
                if (card1 == card2) {
                    card1 = new Cards(); 
                }
                System.out.println(card1);
            }
        }
    }

这是我的支持班:

import java.util.Random;

public class Cards {

    String hearts = "Hearts";
    String diamonds = "Diamonds";
    String clubs = "Clubs";
    String spades = "Spades";

    String suit; 
    int cardNumber; 
    String numberName; 
    String suitName;
    Random randomNum = new Random();

    public Cards () {

    }

    public String suit() {
        int theRandom = randomNum.nextInt(4);

        if (theRandom == 0 ) {           
            suitName = "hearts";
        }
        else if ( theRandom == 1) {
            suitName = "diamonds"; 
        }
        else if (theRandom == 2) {
            suitName = "clubs";
        }
        else {
            suitName = "spades";
        }
        return suitName;
    }
    public String number() {
        int theRandomNum = randomNum.nextInt(12 + 1); 

        if ( theRandomNum == 1 ) {

            numberName = "Ace";
        }
        else if ( theRandomNum == 2) {
            numberName = "2";
        }
        else if ( theRandomNum == 3) {
            numberName = "3";
        }
        else if ( theRandomNum == 4) {
            numberName = "4";
        }
        else if ( theRandomNum == 5) {
            numberName = "5";
        }
        else if ( theRandomNum == 6) {
            numberName = "6";
        }
        else if ( theRandomNum == 7) {
            numberName = "7";
        }
        else if ( theRandomNum == 8) {
            numberName = "8";
        }
        else if ( theRandomNum == 9) {
            numberName = "9";
        }
        else if ( theRandomNum == 10) {
            numberName = "10";
        }
        else if ( theRandomNum == 11) {
            numberName = "Jack";
        }
        else if ( theRandomNum == 12) {
            numberName = "Queen";
        }
        else if ( theRandomNum == 13) {
            numberName = "King";
        } 
        return numberName;
    }

    public String toString()
    {
        if (number() == "null") {
            return ("3" + " of " + suit());
        }

        return (number() + " of " + suit());
    }
}

2 个答案:

答案 0 :(得分:1)

首先你的代码有一个小错误

int theRandomNum = randomNum.nextInt(12 + 1)

同时,根据您的以下代码确定您实际意味着

int theRandomNum = randomNum.nextInt(12)+1

要存储您已有的牌,您只需引入一个字符串并逐步填写,并始终测试您想要接受的牌是否已包含在此字符串中。

//This goes above the loop where you create your cards
String cards = "";

//This goes into the loop
while(cards.contains(card1.toString()){
    card1 = new Cards();
}
cards += card1.toString() + "#"; //Using # as a delimiter
System.out.println(card1);


//At the end you could also print your set of cards
System.out.println(cards);

这不是一个非常好的方法,因为你不允许使用数组或类似的结构,但应该做它的工作。

请记住,班级名称应该是单数。所以不是Cards而是Card

答案 1 :(得分:1)

使用大型字符串存储已选择的卡片:

String cards = "";

for(int i = 0; i < 20; i++)
{
    Cards card = new Cards();
    while(cards.contains(card.toString()))
        card = new Cards(); //keep generating a random card until it's a new card
    cards += card.toString(); //add the card to the string of cards
    System.out.println(card);
}

此代码将进入Driver类的主要方法

相关问题