我使用的是 UML 类图,该程序是关于玩黑 jack 的,我还没有编写驱动程序文件,但有人告诉我我需要在我的默认类中放置一个随机方法。下面是我的代码。 public Card() 是我的默认类,我需要将一个随机方法放入其中,但我不确定我应该怎么做。
import java.util.Scanner;
import java.util.Random;
public class Card
{
private final int MAX = 13;
private int face, suit, points, Spades, Clubs, Diamonds, Hearts, Ace, Jack, Queen, King;
//CONSTRUCTORS
public Card() {
random.newCard();
}
public Card(int faceValue,int suitValue) {
//face
Ace = 1;
Jack = 11;
Queen = 12;
King = 13;
//suit
Hearts = 1;
Diamonds = 2;
Clubs = 3;
Spades = 4;
}
//^^^^^^^
//Mutators
public void setFace(int face)
{
this.face = face;
}
public void setSuit(int suit)
{
this.suit = suit;
}
//Accessors
public int getFace()
{
return face;}
public int getSuit()
{
return suit;}
public int getPoints()
{
return points;}
//^^^^^^^^^^^^^^^^^
public String toString()
{
return "Players cards are " + " ";
}
}
最佳答案
我猜...
Math.random()
对你更有帮助。
int(Math.random()) % 13 + 1; //==> 1 ~ 13
int(Math.random()) % 4 + 1; //==> 1 ~ 4
或
random.nextInt(12) + 1; //==> 1 ~ 13
random.nextInt(3) + 1; //==> 1 ~ 4
会工作。
关于构造函数中的Java随机方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317024/