我正在尝试制作一场战争纸牌游戏。每位玩家一次抽取1张牌,每张牌最多26张。现在我需要将卡的字符串值转换为整数,以便我可以使用它们来确定谁有更高的牌。这是我的第一个使用数组的程序,我在这个程序中遇到了很多问题。我该怎么做呢?
编辑:如果不清楚,我问如何从String []数字中获取int值而无需手动分配任何值。为了看谁赢得每手牌,我需要比较一下。
import java.util.ArrayList;
import java.util.Random;
public class Card {
String finalCard = "";
int suit, number;
String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits
String [] numbers = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" , "Ace" }; //card values
String card = "";
public Card() {
}
public Card(int suits, int numbers)
{
suit = suits;
number = numbers;
}
public String toString()
{
String finalCard = numbers[number] + " of " + suits[suit];
return finalCard;
}
}
import java.util.ArrayList;
import java.util.Random;
public class FullDeck {
private ArrayList<Card> cards = new ArrayList<Card>();//card array list
public FullDeck()
{
for(int a =0; a<=3; a++) //loops through suits
{
for(int b =0; b<=12;b++) //loops through values
{
cards.add(new Card(a,b)); //creates adds cards to list
}
}
}
public Card drawRandomCard()
{
Random generator = new Random(); //picks random card
int index = generator.nextInt(cards.size());
return cards.remove(index); //removes card from list
}
public String toString()
{
String result = "Cards remaining in deck: " + cards; //not currently used
return result;
}
}
import java.applet.Applet;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
public class WarUI extends JApplet
implements ActionListener {
JTextArea displayLabel = new JTextArea(""); //sets label to display message
JButton runButton = new JButton("Run"); //button that starts program
Container con = getContentPane(); //gets container
Card player1;
Card player2;
FullDeck hand1 = new FullDeck();
Card card = new Card();
public void init() {
con.setLayout(new FlowLayout());//sets flowlayout
con.add(new JLabel()); //jlabel container
con.add(runButton); //run button container
con.add(displayLabel); //display label container
runButton.addActionListener(this);//looks to see if run is clicked
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 1; i++) {
player1= hand1.drawRandomCard(); //draws cards for player 1
player1.toString();
}
for (int i = 0; i < 1; i++) {
player2= hand1.drawRandomCard(); //draws cards for player 2
player2.toString();
}
displayLabel.setText(player1.toString() + "\n" + player2.toString() ); //displays both players values/suits
}
}
答案 0 :(得分:1)
首先在Card类中使套装和数字静止。因为每次创建卡时阵列始终保持不变并且不必初始化。虽然你必须在Card类中添加getters到变量suit和number。
要检查哪张卡更高,首先必须检查哪个数字索引更高,然后检查卡的套装索引。
Card card1 = ...
Card card2 = ...
if(card1.getnumber() < card2.getnumber())
{
//card1 is lower
}else if(card1.getnumber() > card2.getnumber()){
//card2 ist lower
}else{
if(card1.getsuit() < card2.getsuit()){
//card1 is lower
}else if(card1.getsuit() > card2.getsuit()){
//card2 is lower
}else{
// Both Cards are the same
}
}
请记住,使用此实现,suit数组的顺序很重要
答案 1 :(得分:0)
要将String
对象转换为原始int
,只需使用Integer.parseInt()
方法,如下所示:
String card1="12";
String card2="20";
int Card1=Integer.parseInt(card1);
int Card2=Integer.parseInt(card2);
if(Card1>Card2)
{
System.out.println("You Win!");
}