类文件错误

时间:2018-02-10 13:25:54

标签: java

我是Java的新手,如果你可以这样称呼我,我遇到了一些麻烦。问题是“int无法转换为PlayingCard”。并且“找不到符号:符号变量随机和”位置类包。

            package playingcard;

            import java.util.Random;

            public class Pack {

                PlayingCard[] cards = new PlayingCard[52];

                public Pack() {
                    // Setting up array
                    String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
                    String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};

                    // Intialising array
                    for (int i = 0; i < cards.length; i++) {
                        cards[i] = i;
                    }
                }

                public void shuffle() {
                    for (int i = 0; i < 52; i++) {
                        Random rnd = new Random();
                        int random = rnd.nextInt(52);
                        cards[i] = Random;
                    }
                }

            //@Override
            //public String toString()
            //{
            //
            //}
            }

2 个答案:

答案 0 :(得分:-1)

您的代码中存在两个问题

首先在这里cards[i]=i;

for (int i = 0; i < cards.length; i++) {
                    cards[i] = i;
                }
  

您无法在cards类型的数组PlayingCard中指定int值   正如您已将cards声明为PlayingCard类型的数组并在integer中插入cards[index]值,这是不可接受的并且会引发错误

第二个问题:

cards[i] = Random;

 for (int i = 0; i < 52; i++) {
                    Random rnd = new Random();
                    int random = rnd.nextInt(52);
                    cards[i] = Random;
                }
  

你没有在任何地方宣布随机,所以,你不能将它分配给阵列卡[]

解决

 for (int i = 0; i < cards.length; i++) {
                cards[i] = new PlayingCard;
            }

现在我们在card []数组中有PlayCard类型的对象,之后你可以做任何你需要做的事情。

无法找到变量随机:

您已为cards[i]分配了值Random,该值显示错误更改:

 public void shuffle() {
                for (int i = 0; i < 52; i++) {
                    Random rnd = new Random();
                    int random = rnd.nextInt(52);
                    cards[i] = rnd;  
                }
            }

要随机播放:

看看@Nomenokes的答案

答案 1 :(得分:-1)

要在数组中设置值,必须将其设置为数组类型的对象,而不是其他任何对象。您正尝试将PlayingCard的广告位设为int,将另一个广告位设为PlayingCard,作为整个广告级Random。这不会奏效。

// Intialising array
  for (int i = 0; i < cards.length; i++) {
        cards[i] = new PlayingCard();
  }

这会产生一个新的PlayingCard并将其放入数组槽中。

for (int i = 0; i < 52; i++) {
     Random rnd = new Random();
     int random = rnd.nextInt(52);
     PlayingCard temp = cards[i]; //puts aside the card at slot i
     cards[i] = cards[random]; //puts the card at random into the slot at i
     cards[random] = temp; //puts the card you set aside into the slot at random
}

按照您的意图,通过交换卡片

进行随机播放