需要帮助从数组中获取int

时间:2013-12-10 06:44:28

标签: java

我有一张战争纸牌游戏。两个用户每人抽取1张卡片,这些卡片将相互比较以查看谁拥有更高的牌。我已完成大部分程序,但我无法弄清楚如何将卡值转移到WarUI。每次我使用我的get方法时它会显示1,所以我一定做错了。数组编号是介于1-13之间的int值,我使用if语句将1更改为ace,将11更改为jack,等等。我在卡类中遇到getValue()问题。我该怎么做?

编辑:如何获取用户卡值,以便我可以将它们相互比较?

import java.util.ArrayList;
import java.util.Random;

public class Card {
        String finalCard = "";
         int suit, number,number2;
        static String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits
        //static String [] numbers2 = { "Ace", "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King"  }; //card values
        static int [] numbers = { 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13  }; //card values
        String card = "";

        public Card() {

        }
         public Card(int suits, int numbers)
                {
                        suit = suits;
                        number = numbers;

                }


         public  int getValue() { // cant get this to work

                 int well = numbers[number];
                 return well;
                }

                public int getSuit() {
                    return suit;
                }
        public String toString()
        {
           String fakeValue = "";
         if (numbers[number] == 1)
            {
                fakeValue = "Ace";
            }
         else if (numbers[number] == 11)
            {
                fakeValue = "Jack";
            }
         else if (numbers[number] == 12)
            {
                fakeValue = "Queen";
            }
         else if (numbers[number] == 13)
            {
                fakeValue = "King";
            }
         else

         fakeValue = Integer.toString(numbers[number]);
                String finalCard = fakeValue + " 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
                JTextArea displayLabel2 = 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
                con.add(displayLabel2); //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()+ card.getValue()); //displays both players values/suits

                    }
            }

2 个答案:

答案 0 :(得分:1)

默认情况下,您没有初始化number,其值为0(实例成员),因此int well = numbers[number];正在给出第一个元素,即1

您可以按照以下方式修改方法,并获取值

           public  int getValue(int position) { 

             int well = numbers[position];
             return well;
           }

答案 1 :(得分:1)

无论何时创建Card,都会调用Card()构造函数。它将您的实例变量编号值设置为0。

public  int getValue() { // cant get this to work

                 int well = numbers[number];
                 return well;
                }

在此方法中,number = 0。 getValue()返回数字[0],即1.使用参数构造函数创建卡片。

public Card(int suits, int numbers)
                {
                        suit = suits;
                        number = numbers;

                }

示例:

Card card = new Card(2,2);