【问题标题】:Java Random method inside a Constructor not working构造函数中的Java随机方法不起作用
【发布时间】:2014-03-11 05:15:55
【问题描述】:

我正在使用 UML 类图,该程序是关于玩二十一点,我还没有编写驱动程序文件,但有人告诉我需要在我的默认类中放置一个随机方法。下面是我的代码。 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 " + " ";

    }

}

【问题讨论】:

  • Random random = new Random(MAX); random.nextInt(); 这就是它的工作原理......
  • "public String toString()" 是你的类中已有的方法,你的“随机”方法可以用同样的方式声明。在此之前,您需要确定此方法将接受哪些参数以及返回什么,具体取决于此随机方法的作用
  • 学习查找 javadocs 并学习如何阅读和理解它们
  • 当你不使用参数时,为什么要将参数传递给构造函数?
  • 我还没有完成,我实际上不知道我应该如何使用它们,这是我的第一个 java 类,我们的书完全是垃圾,不允许使用其他文档来学习东西根据我们的老师

标签: java algorithm class random uml


【解决方案1】:

我猜……

数学.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

将工作。

【讨论】:

    【解决方案2】:

    首先我们定义什么是卡片:

    public class Card
    {
        public final int FACE_Ace = 1;
        public final int FACE_Two = 2;
        public final int FACE_Three = 3;
        public final int FACE_Four = 4;
        public final int FACE_Five = 5;
        public final int FACE_Six = 6;
        public final int FACE_Seven = 7;
        public final int FACE_Eight = 8;
        public final int FACE_Nine = 9;
        public final int FACE_Ten = 10;
        public final int FACE_Jack = 11;
        public final int FACE_Queen = 12;
        public final int FACE_King = 13;
    
        public final int SUIT_Spades = 1;
        public final int SUIT_Clubs = 2;
        public final int SUIT_Diamonds = 3;
        public final int SUIT_Hearts = 4;
    
        public int face;
        public int suit;
    
        public Card (int suit, int face)
        {
            this.suit = suit;
            this.face = face;
        }
    }
    

    那么我们有一套完整的卡组:

    public class Deck
    {
        private Card[] cards;
    
        private Random random;
    
        public Card()
        {
            cards = new Card[52]; // 13 x 4
    
            // fill in all cards
            for (int suit=1; suit<=4; suit++)
                for (int face=1; face<=13; face++)
                    cards[(suit*13)+face-1] = new Card (suit,face);
    
            // seed using Time
            // keeping Random enhance shuffle entropy over usage
            random = new Random (System.currentTimeMillis());
        }
    
        public shuffle ()
        {
            for (int i=0; i<52; i++)
                swap(i, random.nextInt(52)); // [0, 51]
        }
    
        private void swap (int a, int b)
        {
            Card temp = new Card (cards[a].suit, cards[a].face);
    
            cards[a].suit = cards[b].suit;
            cards[a].face = cards[b].face;
    
            cards[b].suit = temp.suit;
            cards[b].face = temp.face;
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果你仍然不能使用数组,随机卡对你没有用,因为你无法检查你是否已经创建了它。

      唯一可用的变体是随机排列。你需要一个算法,它会生成从 1 到 52 的数字,不会重复。

      class RandomPermutation{
          int base=1;
          int next(){
              int result;
              do{
                  base=base*125 /4096;
                  int result=(base-1)/4;
              } while (1<=result<=54)
      
              return result;
          }
      }
      

      这个序列可以生成 0..1023 个置换。您可以更改排列设置您允许的起始基值和范围。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        相关资源
        最近更新 更多