简体   繁体   English

Java-创建卡片组类

[英]Java - Creating a Card Deck Class

I am having trouble with my deck class, i have code that creates a finite deck and will return a value from the array and then make it null. 我的牌组类遇到问题,我有创建有限牌组的代码,该代码将从数组中返回一个值,然后将其设置为null。 it also checks to see if it is null to begin with because getCard() is called multiple times. 它还会检查其开头是否为null,因为多次调用了getCard()。 this ensures there are no duplicate cards. 这样可以确保没有重复的卡。 The problem is that when i run my code it prints empty lines which must mean it is returning null which by my understanding shoudnt be happening. 问题是,当我运行我的代码时,它打印出空行,这必须表示它正在返回null(以我的理解为例)。

public class Deck {

  public int randomNumber;
  private String card;
  private String [] deck;
  public Deck()
  {
    deck = new String [] {"Ace Of Hearts" , "2 Of Hearts" , "3 Of Hearts" , "4 Of Hearts" , "5 Of Hearts" , "6 Of Hearts" , "7 Of Hearts" , 
  "8 Of Hearts" , "9 Of Hearts" , "10 Of Hearts" , "Jack Of Hearts" , "Queen Of Hearts" , "King Of Hearts" ,
  "Ace Of Diamonds" , "2 Of Diamonds" , "3 Of Diamonds" , "4 Of Diamonds" , "5 Of Diamonds" , "6 Of Diamonds" ,
  "7 Of Diamonds" , "8 Of Diamonds" , "9 Of Diamonds" , "10 Of Diamonds" , "Jack Of Diamonds" , "Queen Of Diamonds" ,
  "King Of Diamonds" , "Ace Of Clovers" , "2 Of Clovers" , "3 Of Clovers" , "4 Of Clovers" , "5 Of Clovers" ,
  "6 Of Clovers" , "7 Of Clovers" , "8 Of Clovers" , "9 Of Clovers" , "10 Of Clovers" , "Jack Of Clovers" ,
  "Queen Of Clovers" , "King Of Clovers" , "Ace Of Spades" , "2 Of Spades" , "3 Of Spades" , "4 Of Spades" , "5 Of Spades" ,
  "6 Of Spades" , "7 Of Spades" , "8 Of Spades" , "9 Of Spades" , "10 Of Spades" , "Jack Of Spades" ,
  "Queen Of Spades" , "King Of Spades"};
  }

  public String getCard ()
  {
    randomNumber = (int) (Math.random() * 51) + 1;
    String tempCard;
    if (deck[randomNumber] != null)
    {
      tempCard = deck[randomNumber];
      deck[randomNumber] = ("");
      return (tempCard); 
    }
else
{
  while (deck[randomNumber] == null)
  {
    randomNumber = (int) (Math.random() * 51) + 1;
  }
  tempCard = deck[randomNumber];
  deck [randomNumber] = ("");
  return (tempCard);
  }
  }
}

here is a test program to run my deck class: 这是运行我的甲板课程的测试程序:

import hsa.Console;

public class DeckTest {
static Console c;

  public static void main(String[] args) {

//The Console 
c = new Console();

Deck Card = new Deck();

c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
c.println(Card.getCard());
  }
 }

This test is futile: 这个测试是徒劳的:

deck[randomNumber] != null

because all of your deck elements aren't null and you are replacing drawn cards with "", which is the empty string, but not null. 因为您所有的牌组元素都不为空,并且您将用“”代替抽奖牌,“”为空字符串,但不为null。

Use 采用

 deck [randomNumber] = null;

Also the loop should be improved. 此外,还应改善循环。

private Random rand = new Random();
public String getCard () {
    String tempCard = null;
    do {
        int r = rand.nextInt( deck.length );
        tempCard = deck[r];
        deck[r] = null;
    } while( tempCard == null );
    return tempCard; 
}

A test should be added so as not to try to draw more than there are in the deck. 应该添加一个测试,以免绘制的内容超过甲板上的内容。

There are several things that can be improved with your code. 您的代码有几处可以改进的地方。

To solve your problem: "" is not the same as null . 解决您的问题: ""null The former is a non-null empty string. 前者是一个非空的空字符串。 The latter is a pointer that points to nothing. 后者是一个指向什么都没有的指针。 You will need to use the latter to have your null-check str == null work as you intend. 您将需要使用后者使null-check str == null正常工作。

Arrays are 0-indexed. 数组是0索引的。 For some reason, in your random number you are adding 1, making the Ace of Hearts be a card that can never be selected. 出于某种原因,在您的随机数中您将添加1,从而使Ace of Hearts成为永远无法选择的卡片。 What you want instead is to make sure you are multiplying by the size of the number of elements. 相反,您要确保要乘以元素数量的大小。 That will create a uniformly distributed number from [0, size) and using a cast to integer will give you the correct number. 这会从[0, size)创建一个均匀分布的数字[0, size)并使用强制转换为整数将为您提供正确的数字。

I would also suggest looking at a do-while loop instead of an if statement with a while loop. 我还建议您查看do-while循环,而不是带while循环的if语句。 That would significantly clean up your code. 这将大大清理您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM