简体   繁体   English

我可以使方法从LinkedList中的值中忽略字符吗?

[英]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. 我对Java还是很陌生,正在从事一个项目,一个纸牌游戏。

I've made a full deck of cards using a LinkedList . 我已经使用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. 事实是,我在所有52张卡片上都以“♥”等形式使用面孔。

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 ). 同时也是LinkedList的玩家得到了两张牌,它们是随机的,并且以1♥的形式出现(这是该牌在LinkedList的值)。

The problem is getting my Method containing of a switch to calculate the values. 问题是让我的Method包含一个用于计算值的开关。 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: 这是我的getScore方法:

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中的面部字符。

String s = card.substring(1);

So if your card looked like this: 因此,如果您的卡片如下所示:

"♥A"

substring will remove the substring将删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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