English 中文(简体)
我如何写“假报”,在复制后复制一张卡。
原标题:How do I write an if-statement to repick a card after getting a duplicate

I need to create a program that randomly picks 2 cards and writes the total of them at the end This is the code so far

/*
 * RandomCard.java
 * -------------------
 * Displays the name of a card randomly chosen from a complete deck of 52 
 * playing cards.
 */
import java.util.Random;

public class RandomCard {
    private static Random rgen = new Random(); /* random generator

    /**
     * Shows the name of a randomly selected card from 52 cards
     */
    public static void main(String [] args) {
        System.out.println("Grab 2 cards");
        Card randomCard = selectCardFromDeckRandomly();
        System.out.println(randomCard);
        System.out.println(randomCard);
    }

    /*
     * Randomly select a card from a card deck
     */
    private static Card selectCardFromDeckRandomly() {
        String rank = selectRandomCardRank();
        String suit = selectRandomCardSuit();
        return (new Card(rank, suit));  
    }

    /*
     * Randomly select a card number
     */
    private static String selectRandomCardRank() {
        String cardRank;
        int r = 1 + rgen.nextInt(13);
        switch (r) {
        case 1:
            cardRank = "Ace";
            break;
        case 2:
            cardRank = "2";
            break;
        case 3:
            cardRank = "3";
            break;
        case 4:
            cardRank = "4";
            break;
        case 5:
            cardRank = "5";
            break;          
        case 6:
            cardRank = "6";
            break;
        case 7:
            cardRank = "7";
            break;
        case 8:
            cardRank = "8";
            break;
        case 9:
            cardRank = "9";
            break;
        case 10:
            cardRank = "10";
            break;
        case 11:
            cardRank = "Jack";
            break;
        case 12:
            cardRank = "Queen";
            break;          
        default:
            cardRank = "King";
            break;
        }
        return cardRank;
    }

    /*
     * Randomly select a card suit
     */
    private static String selectRandomCardSuit() {
        String cardSuit;
        int s = 1 + rgen.nextInt(4);
        switch (s) {
        case 1:
            cardSuit = "Clubs";
            break;
        case 2:
            cardSuit = "Diamonds";
            break;
        case 3:
            cardSuit = "Hearts";
            break;
        default:
            cardSuit = "Spades";
            break;
        }
        return cardSuit;
    }
}

Class Code:

/*
 * Card.java
 * to declare the types of cards
 */
 public class Card {
    private String rank;   /* The card rank : Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King */
    private String suit;   /* The card suit : Clubs, Diamonds, Hearts, Spades */

    /* Constructor
     * 
     * Creates an instance object of class Card with the specified rank and suit
     *rnk: The rank of the card
     *sut: The suit of the card
     */
    public Card(String rnk, String sut) {
        rank = rnk;
        suit = sut;
    }

    public String toString() {
        return (rank + " of " + suit);
    }
}

I know simple if-statements, but I don t know how to do this one.

问题回答

"How do I write an if-statement to repick a card after getting a duplicate ..."

页: 1 我需要制定一项方案,随机抽取2张贺卡,并在结尾处写出全部卡片。

... 我知道简单的说法,但我不知道如何做这种说法。”

我建议使用<<>Collection,以维护Card物体。

http://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/HashMap.html

static class Deck<K, V> extends HashMap<Card, Integer> {
    Deck() {
        int i, j;
        char suit;
        char[] suits = {  C ,  D ,  H ,  S  };
        for (i = 0; i < suits.length; i++) {
            suit = suits[i];
            for (j = 1; j <= 13; j++)
                put(new Card(rankOf(j), suit), j);
        }
    }

    Integer put(char rank, char suit) {
        return super.put(new Card(rank, suit), valueOf(rank));
    }
    
    Integer get(char rank, char suit) {
        return super.get(new Card(rank, suit));
    }

    static int valueOf(char rank) {
        return switch (rank) {
            case  A  -> 1;
            case  T  -> 10;
            case  J  -> 11;
            case  Q  -> 12;
            case  K  -> 13;
            default -> Character.digit(rank, 10);
        };
    }

    static char rankOf(int value) {
        return switch (value) {
            case 1 ->  A ;
            case 10 ->  T ;
            case 11 ->  J ;
            case 12 ->  Q ;
            case 13 ->  K ;
            default -> Character.forDigit(value, 10);
        };
    }
}

uff,你可以创造内层,可以提供装满的Map.Entry/em>数值清单。

class Shuffle {
    List<AbstractMap.SimpleEntry<Card, Integer>> list = new ArrayList<>();
    Iterator<AbstractMap.SimpleEntry<Card, Integer>> iterator;

    Shuffle() {
        for (Map.Entry<Card, Integer> e : entrySet())
            list.add(new SimpleEntry<>(e));
        Collections.shuffle(list);
        iterator = list.iterator();
    }

    Card next() {
        return iterator.hasNext() ? iterator.next().getKey() : null;
    }

    List<Card> next(int amount) {
        List<Card> list = new ArrayList<>();
        for (int i = 0; iterator.hasNext() && i < amount; i++)
            list.add(iterator.next().getKey());
        return list.size() != 0 ? list : null;
    }

    List<Card> remaining() {
        List<Card> list = new ArrayList<>();
        while (iterator.hasNext()) list.add(iterator.next().getKey());
        return list.size() != 0 ? list : null;
    }
}

这方面的一个例子是使用。

Deck<Card, Integer>.Shuffle shuffle = new Deck().new Shuffle();
shuffle.next(10).forEach(System.out::println);

产出

T of S
8 of C
A of H
4 of C
3 of C
K of H
T of H
A of C
5 of S
8 of D

最后,用static> ValueOf Deck 方法添加这两张卡。

Deck<Card, Integer>.Shuffle shuffle = new Deck().new Shuffle();
Card a, b;
int x, y;
for (int i = 0; i < 10; i++) {
    a = shuffle.next();
    b = shuffle.next();
    System.out.printf(" %s  (%s) + ", a, x = Deck.valueOf(a.rank));
    System.out.printf(" %s  (%s) = ", b, y = Deck.valueOf(b.rank));
    System.out.printf("%d%n", x + y);
}

产出

 K of C  (13) +  J of H  (11) = 24
 6 of H  (6) +  4 of S  (4) = 10
 3 of C  (3) +  2 of H  (2) = 5
 9 of S  (9) +  6 of S  (6) = 15
 7 of C  (7) +  J of D  (11) = 18
 4 of C  (4) +  8 of C  (8) = 12
 Q of H  (12) +  8 of S  (8) = 20
 2 of D  (2) +  9 of C  (9) = 11
 K of S  (13) +  A of S  (1) = 14
 Q of D  (12) +  J of D  (11) = 23




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签