Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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 - Fatal编程技术网

Java 如何在不生成数组的情况下检查重复值?

Java 如何在不生成数组的情况下检查重复值?,java,Java,我有一个程序,根据随机数的值发20张牌。到目前为止,它适用于处理黑桃王、红桃2等卡牌。我的工作是使用方法检查卡牌是否重复,但不使用数组。以下是我检查重复项的解决方案,由于明显的原因,重复项不起作用: public class Driver { public static void main(String [] args) { for (int i = 0; i < 20; i++){ Cards card1

我有一个程序,根据随机数的值发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);
            }
        }
    }

首先,您的代码有一个小错误

int-therrandomnum=randomNum.nextInt(12+1)

然而,根据下面的代码确定您的实际意思

int-therrandomnum=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);
这不是一个很好的方法,因为不允许使用数组或类似的结构,但应该完成它的工作


请记住,类名应该是单数的。因此,不是
,而是

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

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);
}
String cards=”“;
对于(int i=0;i<20;i++)
{
卡片=新卡片();
while(cards.contains(card.toString()))
card=new Cards();//继续生成一张随机卡,直到它成为一张新卡为止
cards+=card.toString();//将卡添加到卡串中
系统输出打印项次(卡片);
}

此代码将进入驱动程序类的主方法中

非常确定您需要一个数组来完成此操作。或者20个不同的卡片对象。问题是,我的java书在教你数组之前,把这个问题作为练习。我的老师说我们不允许使用数组。如果不允许使用数组,请使用ArrayList-将所有卡片放入其中,然后用一张卡片进行迭代,您想检查我是否可以使用contains方法来解决此问题?我的值是字符串。让我们来看看。您不需要添加#,在
11 of Clubs
上触发
1 of Clubs
应该不会有任何冲突,因为1是Ace,11是Jack@phflack将其视为防御编程的一部分;-)但从技术上讲你是对的。另一个问题是,如果你得到了重复的,你将无法生成完整的20张卡。查看我的答案,了解比使用if语句更简单的方法另一个问题,看起来并不是每次都初始化card1,除非您从原始代码中保留card1和card2?这些部分是要插入到其原始代码中的。仅仅为了三行代码而在这里复制他的全部代码是没有帮助的。
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);
}