Another Java coder novice requiring approval

So ... Recently, I am learning Java, and I want you to help me with my very first program, if you want. This is my code for a program that displays a random card from the deck.

My question is this: is there a smarter way to get this program to work?

/*
 * File: RandomCard.java
 * ----------------
 * This program display random card from the deck with it own rank
 * (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Quenn, King) and suit (Clubs,
 * Diamonds, Hearts, Spades).
 */

import acm.program.*;
import acm.util.*;

public class RandomCard extends ConsoleProgram {
    /* Run the program */
    public void run() {
        println("This program displays a random card from the deck.");
        println("You random card is " + getRandomRank() + " "
                + getRandomSuit() + ".");       
    }

    /* Get random rank for the card. */
    private String getRandomRank() {
        int rank = rgen.nextInt(1, 13);
        switch (rank) {
            case 1: return "Ace";
            case 2: return "2";
            case 3: return "3";
            case 4: return "4";
            case 5: return "5";
            case 6: return "6";
            case 7: return "7";
            case 8: return "8";
            case 9: return "9";
            case 10: return "10";
            case 11: return "Jack";
            case 12: return "Queen";
            case 13: return "King";
            default: return null;
        }
    }

    /* Create random suit from within Clubs, Diamonds, Hearts and Spades. */
    private String getRandomSuit() {
        int suit = rgen.nextInt(0, 3);
        switch (suit) {
            case 0: return "Clubs";
            case 1: return "Diamonds";
            case 2: return "Hearts";
            case 3: return "Spades";
            default: return null;
        }
    }

    /* Create an instance variable for the random number generator */
    private RandomGenerator rgen = new RandomGenerator();
}
+3
source share
4 answers

A slight improvement: every time you have a switch statement, for example:

 switch (rank) {
        case 1: return "Ace";
        case 2: return "2";
        case 3: return "3";
        case 4: return "4";
        case 5: return "5";
        case 6: return "6";
        case 7: return "7";
        case 8: return "8";
        case 9: return "9";
        case 10: return "10";
        case 11: return "Jack";
        case 12: return "Queen";
        case 13: return "King";
        default: return null;
    }

Instead, you can declare an array:

String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
                   "Jack", "Queen", "King" };

And then:

if (rank < 1 || rank > 13) return null;
return ranks[rank - 1]; // arrays are zero-based
+4
source

Instead of returning Stringfor Rank and Suite, instead, I would make them enum, so they are typical.

public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

, : http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html

+4

, .

, , : ? , - .

, :
: @JUST @Daniel switch .

, , .

, , ​​ . , , , () ( , , ). , , .

, "" xml json.

"" .

, , , . .

+3

Using this approach with independent enumeration lists does not work. This may be enough for the application you created, but if you create some kind of card game, you will have double cards, such as two aces of hearts. I suggest you create a class map that contains the rank and set as enumerations. Create a Deck class that initializes the creation of all cards for the deck. Then you can have the Card drawNextRandom () method, which returns the next random card from the deck and removes it from the deck.

public class Card{
  public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
  public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };

  private Rank rank;
  private Suite suite;

  public Card(Rank rank, Suite suite){
    this.rank = rank;
    this.suite = suite;
  }

  // don't forget getters, equals and hashcode
}

public class Deck{

  private RandomGenerator rgen = new RandomGenerator();

  private List<Card> cards = new ArrayList<Card>();

  public Deck(){
    for(Rank rank : Rank.values()){
      for(Suite suite : Suite.values()){
        cards.add(new Card(rank, suite));
      }
    }
  }

  public Card nextRandomCard(){
    int nextCard = rgen.nextInt(0, cards.size());
    Card card = cards.remove(nextCard);  
    return card;
  }
}

This will be the starting point for a card game.

Cheers, Sven

0
source

Source: https://habr.com/ru/post/1793256/


All Articles