現在ブラックジャックを作っているのですが、カードデッキクラスの表示がランダムで出なくてはいけないのに
すべてハートの13になってしまいます。いろいろと試してはいるのですがどこに問題があるのでしょうか。
public class Card { static final int RANK = 13; static final int[] SCORESTR = {1,2,3,4,5,6,7,8,9,10,10,10,10}; static final String[] NUMSTR = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; static final String[] SUITSTR = {"♠","♦","♣","♥"}; private static int n; public Card (int n) { Card.n = n; } //物理番号 1~52 public int getNum() { return n; } //カードの札番号 1~13 public int cardNumber() { return (n-1)%RANK+1; } //種類番号 0~3 public int suitNumber() { return (n-1)/RANK; } //札の点数をかえす public int score() { return SCORESTR[cardNumber()-1]; } //札番号の文字列表現を返す public String cardNumberString() { return NUMSTR[cardNumber()-1]; } //札の種類の文字列表現を返す public String suitName() { return SUITSTR[suitNumber()]; } //クラスの文字列を返す public String toString() { return suitName()+cardNumberString(); } public static void main(String[] args) { //Cardクラス確認用 for(int i = 1; i <= 52; i++) { Card c = new Card(i); //カードクラスのインスタンス System.out.printf("番号(1~52)=%2d",c.getNum()); System.out.printf("札=%2d",c.cardNumber() ); System.out.printf("種類=%1d",c.suitNumber() ); System.out.println(c); } } } ```上記はカードクラスです。 ```ここに言語を入力 import java.util.ArrayList; public class CardDeck { private int max; private ArrayList<Card> deck; public CardDeck(int max) { //コンストラクタ max = 52; this.max = max; deck = new ArrayList<Card>(52); initialize(); } //deckの中身削除 deckにカードをmax枚まで追加 public void initialize(){ deck.clear(); //中身消去 for(int i = 0; i < max; i++) { Card a = new Card(i+1); deck.add(a); //カード追加 } } //現在の残りのカード枚数を返す public int size() { return deck.size(); //残りのカード枚数 } //デッキから1枚のカードを取り出すときに使う public Card next() { int a = size(); //残りのカード枚数 if(a == 0) { //残りが0枚ならnullを返す。 return null; } int b = (int)(Math.random()*a); Card c = deck.remove(b); return c; }
Crad nextメソッドがおかしいのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/28 11:24