英文:
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<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 "));
答案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 = "3 Spades"
String[] cardSplit = str.split(" ");
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+" "+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<Card> cards=new ArrayList<>(Arrays.<Card>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);
Output:
Your hand:[Ass Herz, 2 Herz]
has a total value of:3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论