2016-04-03 8 views
1

出力が得られず、ここからどこに行くのかわかりません。メソッドと複数のクラスを使用するカードのデッキ

標準のトランプカードを表すCardというクラスを設計して実装します。各カードにはスーツと額面があります。次に、Cardクラスの52個のオブジェクトを配列に格納するドライバクラスを作成します。デッキをシャッフルし、カードを処理し、デッキに残っているカードの数を報告する方法を含めます。シャッフル方法はフルデッキを想定する必要があります。主な方法は、シャッフルされたデッキから各カードを処理し、処理された各カード(スーツとフェイスバリュー)を印刷する必要があります。

は、ここで私がこれまで持っているものです:Javaで

import java.util.Random; 
public class card { 
public static void main(String[] args) {} 

    public class deck { 


     int[] deck = new int[52]; 
     String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"}; 
     String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; 

     public void create() {//initialize cards 
      for (int i = 0; i < deck.length; i++) deck[i] = i; 
     } 


     public void shuffle() {//deck shuffle 
      for (int i = 0; i < deck.length; i++) { 

       int index = (int) (Math.random() * deck.length); 
       int temp = deck[i]; 
       deck[i] = deck[index]; 
       deck[index] = temp; 
      } 



       //display all the cards! 
       for (int i = 0; i < deck.length; i++) 

       { 
        String suit = suits[deck[i]/13]; 
        String rank = ranks[deck[i] % 13]; 
        System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit); 


       } 


      } 
     } 

     } 
+1

無関係ですが、一貫した字下げを使用して疑似空白を削除すると、コードを読みやすく、理由が簡単になります。 –

答えて

3

、あなたのコードが実行を開始する場所を主な方法は()です。名前には、それがJavaで良い習慣です、

public static void main(String[] args) { 
deck myDeck = new deck(); 
myDeck.create(); 
myDeck.shuffle(); 
} 

注意点としては:

public static void main(String[] args) {} 

、それが何かを得る、このようなものに変更する:あなたは今、あなたのmainメソッドでは何もありません大文字のクラス。ここで

+0

私はそれをして、それは私の新しいデッキは静的なコンテキストから参照することはできません私に言っている。私は何が恋しいの? – whatamii

+0

私のソリューションがあなたの問題を解決するのに役立つならば、解決策としてマークした方が良いでしょう。 – nhouser9

0

は、私は、あなたのためにいくつかのコードを記述しようとしたその完全ではないが、あなたは継続するためのフレームワークを提供し、あなたがしなければならないすべてはあなた自身のシャッフルの方法を実装し、ランクに複数の変数を追加します:

public class CardDriver { 
    public static void main(String[] args) { 

     Deck myDeck = new Deck(); 
     myDeck.shuffle(); 
     System.out.println("Dealt cards are:"); 
     myDeck.dealAllCards(); 
    } 
} 

class Card { 
    String suite, faceValue; 
    Card(String suite, String faceValue) { 
     this.suite = suite; 
     this.faceValue = faceValue; 
    } 
    void printCard() { 
     System.out.println(faceValue + " " + suite); 
    } 
} 

class Deck { 
    String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" }; 
    String[] ranks = { "6", "7" }; 
    int count = ranks.length * suits.length; 
    Card deck[] = new Card[count]; 

    Deck() { // fills out the deck 
     int index = 0; // this is for simple deck filling 
     for (int i = 0; i < ranks.length; i++) { // for ranks 
      for (int j = 0; j < suits.length; j++) { // for suits 
       deck[index] = new Card(suits[j], ranks[i]); 
       index++; 
      } 
     } 
    }; 

    void shuffle() { // shuffles the deck 
     // your code here 
    } 

    Card dealCard() { // gives card from deck 
     if (returnLeft() > 0) { 
      count--; 
      return deck[count]; 
     } else return null; 
    } 

    int returnLeft() { 
     return count; 
    } 

    void dealAllCards() { 
     int counter = count; 
     for (int i = 0; i < counter; i++) { 
      Card someCard = dealCard(); 
      someCard.printCard(); 
     } 
    } 
} 
関連する問題