문제

So I've been learning java recently using the latest version of Bluej as was told this would be a good starting point. I've done the usual Hello World, learning how to get and set, arrays and array lists etc... I decided to give the simple card game a go which I do believe is actually Blackjack but don't quote me on that. Anyway not really knowing where to start I got a template but am having trouble with actually trying to figure out some parts of it, hoping someone can help me out here code is as follows:

public class Deck()
{
    private int card;
    private int suit;

public Deck()
{
    shuffle();
}

public void shuffle()
{
   double rand = (int)(Math.random()*(card+1)
   // Bare in mind I dont even think this is correct
   // Need to generate a double value from 0.0 up to but not including 1.0
   // Apparently to produce value in a range of 1 to 6
   // I need to apply a scaling factor of 6 and add 1 to the result
}

public int getCard()
{
    return card;
}

public int getSuit()
{
    return suit;
}

So is my code even right? I don't want it to be a complicated version of blackjack if indeed it is that which judging by the fact its 52 cards in the pack and I think 4 cards per hand or something like that it is very likely as I've done some research too to see what others have produced but I really have no idea where I'm going with this one! Any help very much appreciated! As I really want to fill in the gaps!

도움이 되었습니까?

해결책

Some mistakes:

  • Class body: its public class Deck{} instead of public class Deck(){}. Only use () for methods or constructors, not for classes.

  • You declare the fields card and suit, but you never actually set them to anyhting.

  • double rand: you declare the variable rand as a double, but lateron you try to assign an int to it

Tips:

  • for generating random values, try the "Random" class. This class has a a nextInt(n) method that can generate numbers between 0 and n-1. It's easier to understand and it's cleaner than Math.random()

  • Try to make an extra class "Card" that contains a numbers representing the card's number and the suite.

  • It will be easier if you represent the cards in your deck in an array or an ArrayList. The last one can be used in the Collections.shuffle() method. This way you won't have to dabble with Random or Math.random().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top