将由数字和ArrayList中的单词组成的字符串转换为整数,Java。

huangapple go评论84阅读模式
英文:

Convert String made out of numbers and words of ArrayList to int java

问题

以下是翻译好的部分:

我是一个 Java 初学者,正在尝试运行一个控制台二十一点游戏。
我创建了一个包含卡牌的字符串的 ArrayList,比如 "3 Spades"。
现在我需要检查卡牌的点数,以免超过 21 点。
我已经尝试将所有字符串转换为整数,但对我来说没有起作用。
有没有一种方法可以从字符串中提取出只有数字,并保存为整数?
谢谢你的帮助。

public ArrayList<String> Karten() {
    ArrayList<String> Herz = new ArrayList<String>(
            Arrays.asList(" 2 Herz "," 3 Herz "," 4 Herz "," 5 Herz "," 6 Herz "," 7 Herz "," 8 Herz "," 9 Herz "," 10 Herz ", " Bube Herz "," Dame Herz "," König Herz "," Ass Herz "));

请注意,由于您要求不回答关于翻译内容的问题,我将只返回翻译好的文本,不做其他回答。如果您需要进一步的帮助,请随时提问。

英文:

I am a Java beginner and trying to get a console blackjack to run.
I created an ArrayList with cards as Strings, such as "3 Spades".
Now I need to check for the values so that I dont get more than 21 for example.
I have tried giving all of the Strings ints but it that isnt working for me.
Is there a way to filter only the numbers out of the String and save as an int?
Thanks for your help.

public ArrayList&lt;String&gt; Karten () {
	ArrayList&lt;String&gt;Herz = new ArrayList&lt;String&gt;(
			Arrays.asList(&quot; 2 Herz &quot;,&quot; 3 Herz &quot;,&quot; 4 Herz &quot;,&quot; 5 Herz &quot;,&quot; 6 Herz &quot;,&quot; 7 Herz &quot;,&quot; 8 Herz &quot;,&quot; 9 Herz &quot;,&quot; 10 Herz &quot;, &quot; Bube Herz &quot;, &quot; Dame Herz &quot;, &quot; K&#246;nig Herz &quot;, &quot; Ass Herz &quot;));

答案1

得分: 2

当您获得一张卡片时,请尝试以下操作:

String card = "3 Spades";
String[] cardSplit = card.split(" ");
int cardValue = Integer.parseInt(cardSplit[0]);
String cardType = cardSplit[1];

现在您可以将cardValue用作整数,并将其加入总数。如果您需要卡片的类型,您也可以引用cardType,但假设这是21点游戏,我认为这并不重要。

如果有帮助,请告诉我。

英文:

When you get a card, try the following:

String card = &quot;3 Spades&quot;
String[] cardSplit = str.split(&quot; &quot;);
int cardValue = Integer.parseInt(cardSplit[0]);
String cardType = cardSplit[1];
 

You can now use cardValue as an integer and add it to a total. You can also reference cardType if you need the type of card, but assuming this is Black Jack I don't think it matters much.

Let me know if this helps.

答案2

得分: 2

你还可以创建一个新类型的卡片(Card),它具有一个数值字段:

public class Card {
    private int value;
    private String valueLabel;
    private String shape;
    
    public Card(int value, String valueLabel, String shape) {
        this.value = value;
        this.valueLabel = valueLabel;
        this.shape = shape;
    }
    
    public int getValue() {
        return value;
    }
    
    public String toString() {
        return valueLabel + " " + shape;
    }
}

这也解决了你在 "Bube Herz"、"Dame Herz"、"König Herz"、"Ass Herz" 这些没有数字的情况下的问题。

现在你可以将你的手牌构建为卡片实例的列表:

ArrayList<Card> cards = new ArrayList<>(Arrays.asList(
    new Card(1, "Ass", "Herz"),
    new Card(2, "2", "Herz")
));

int totalValue = cards.stream().mapToInt(c -> c.getValue()).sum();
System.out.println("Your hand:" + cards);
System.out.println("has a total value of:" + totalValue);

输出结果:

Your hand:[Ass Herz, 2 Herz]
has a total value of:3
英文:

You can also create a new type Card which has a numeric value field:

public class Card {
    private int value;
    private String valueLabel;
    private String shape;
    public Card(int value, String valueLabel, String shape) {
        this.value=value;
        this.valueLabel=valueLabel;
        this.shape=shape;
    }
    public int getValue() {
        return value;
    }

    public String toString() {
        return valueLabel+&quot; &quot;+shape;
    }
}

This also solves your problem for " Bube Herz ", " Dame Herz ", " König Herz ", " Ass Herz " which don't have a number.

Now you can build your hand as a list of Card instances:

ArrayList&lt;Card&gt; cards=new ArrayList&lt;&gt;(Arrays.&lt;Card&gt;asList(new Card(1,&quot;Ass&quot;,&quot;Herz&quot;), new Card(2,&quot;2&quot;,&quot;Herz&quot;)));
int totalValue=cards.stream().mapToInt(c-&gt;c.getValue()).sum();
System.out.println(&quot;Your hand:&quot;+cards);
System.out.println(&quot;has a total value of:&quot;+totalValue);

Output:

Your hand:[Ass Herz, 2 Herz]
has a total value of:3

huangapple
  • 本文由 发表于 2020年8月26日 20:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63597900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定