简体   繁体   中英

Can I make a method ignore a character from a value in a LinkedList?

I'm very new to Java and I'm working on a project, a card game.

I've made a full deck of cards using a LinkedList . I'm not so sure this is the best solution anymore, but I've gone too far to turn back now.

The thing is that I'm using faces on all my 52 cards in the form of "♥" etc.

The player who is also a LinkedList gets dealt two cards, and they're random and comes out the form of 1♥ (this is the value the card has in the LinkedList ).

The problem is getting my Method containing of a switch to calculate the values. Is there a way to ignore the "♥"? It works perfectly fine when I remove the faces, but I don't wanna do that! When the faces are left in, the switch does nothing.

Here is my getScore method:

public static int getScore(String card, int score) {
    int temp = card.indexOf("");
    String face = card.substring(temp);

    switch (face) {
        case "A":
            score += 1;
            break;
        case "2":
            score += 2;
            break;

etc.

I hope there's an easy way to make this work?

You need to ignore the face character inside the String.

String s = card.substring(1);

So if your card looked like this:

"♥A"

substring will remove the

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM