打印存储在一维数组中的对象的特定字符串

时间:2017-07-13 03:27:48

标签: arrays object

我正在尝试打印存储在Array索引中的Token对象的String名称。我得到的只是令牌@,看起来像是一个位置地址。我希望它打印该对象的字符串。

import java.util.Scanner;

public class MainBoardDriver 
{    
    public static void main(String[] args) 
    {           
        Scanner scan = new Scanner(System.in);

        Squares[] squareArray = new Squares[40]; // Creates an array to store properties

        squareArray[0] = new GoSquare("Gp");
        squareArray[1] = new PropertySquares("Mediterranean Avenue", 60, "purple");
        squareArray[2] = new CommunityChestSquares("Community Chest");
        squareArray[3] = new PropertySquares("Baltic Avenue", 60, "purple");
        squareArray[4] = new TaxSquares("Income Tax", 200);
        squareArray[5] = new RailroadSquares("Reading Railroad", 200);
        squareArray[6] = new PropertySquares("Oriental Avenue", 100, "Light Blue");
        squareArray[7] = new ChanceSquares("Chance");
        squareArray[8] = new PropertySquares("Vermont Avenue", 100, "Light Blue");
        squareArray[9] = new PropertySquares("Connecticut Avenue", 100, "Light Blue");
        squareArray[10] = new InJailJustVisitingSquare("In Jail or Just Visiting");
        squareArray[11] = new PropertySquares("St. Charles Place", 140, "Pink");
        squareArray[12] = new UtilitySquares("Electric Company", 150);
        squareArray[13] = new PropertySquares("States Avenue", 140, "Pink");
        squareArray[14] = new PropertySquares("Virginia Avenue", 160, "Pink");
        squareArray[15] = new RailroadSquares("Pennsylvania Railroad", 200);
        squareArray[16] = new PropertySquares("St. James Place", 180, "Orange");
        squareArray[17] = new CommunityChestSquares("Community Chest");
        squareArray[18] = new PropertySquares("Tennessee Avenue", 180, "Orange");
        squareArray[19] = new PropertySquares("New York Avenue", 200, "Orange");
        squareArray[20] = new FreeParkingSquare("Free Parking");
        squareArray[21] = new PropertySquares("kentucky Avenue", 220, "Red");
        squareArray[22] = new ChanceSquares("Chance");
        squareArray[23] = new PropertySquares("Indiana Avenue", 220, "Red");
        squareArray[24] = new PropertySquares("Illinois Avenue", 240, "Red");
        squareArray[25] = new RailroadSquares("B. & O. Railroad", 200);
        squareArray[26] = new PropertySquares("Atlantic Avenue", 260, "Yellow");
        squareArray[27] = new PropertySquares("Ventnor Avenue", 260, "Yellow");
        squareArray[28] = new UtilitySquares("Water Works", 150);
        squareArray[29] = new PropertySquares("Marvin Gardens", 280, "Yellow");
        squareArray[30] = new GoToJailSquare("Go To Jail");
        squareArray[31] = new PropertySquares("Pacific Avenue", 300, "Green");
        squareArray[32] = new PropertySquares("North Carolina Avenue", 300, "Green");
        squareArray[33] = new CommunityChestSquares("Community Chest");
        squareArray[34] = new PropertySquares("Pennsylvania Avenue", 320, "Green");
        squareArray[35] = new RailroadSquares("Short Line", 200);
        squareArray[36] = new ChanceSquares("Chance");
        squareArray[37] = new PropertySquares("Park Place", 350, "Blue");
        squareArray[38] = new TaxSquares("Luxuary Tax", 100);
        squareArray[39] = new PropertySquares("Boardwalk", 400, "Blue");

        System.out.println("Enter the number of players: ");

        int numPlayers = scan.nextInt();

        Token[] tokenArray = new Token[8]; // Creates an array to store objects

        tokenArray[0] = new Token("Battle Ship", 0, 1500);
        tokenArray[1] = new Token("Top Hat", 0, 1500);
        tokenArray[2] = new Token("Dog", 0, 1500);
        tokenArray[3] = new Token("Boot", 0, 1500);
        tokenArray[4] = new Token("Wheel Barrow", 0, 1500);
        tokenArray[5] = new Token("Race Car", 0, 1500);
        tokenArray[6] = new Token("Thimble", 0, 1500);
        tokenArray[7] = new Token("Cat", 0, 1500);

        for(int i = 1; i <= numPlayers; i++) // loop to for players to chose a token
        {

            System.out.println("Player " + i + " Chose a token: ");
            int userChoice = scan.nextInt();
            System.out.println("You chose: " + tokenArray[userChoice].gettoString()); // 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

    int numPlayers = 0;

    do
    {
    System.out.println("Enter the number of players between 2 and 6: ");

    numPlayers = scan.nextInt();
    }
    while(numPlayers < 2 || numPlayers > 6);

    Token a = new Token("Battle Ship", 0, 1500, 0);
    Token b = new Token("Top Hat", 0, 1500, 0);
    Token c = new Token("Dog", 0, 1500, 0);
    Token d = new Token("Boot", 0, 1500, 0);
    Token e = new Token("Wheel Barrow", 0, 1500, 0);
    Token f = new Token("Race Car", 0, 1500, 0);
    Token g = new Token("Thinble", 0, 1500, 0);
    Token h = new Token("Cat", 0, 1500, 0);

    a.t=1;
    b.t=2;
    c.t=3;
    d.t=4;
    e.t=5;
    f.t=6;
    g.t=7;
    h.t=8;

    Token[] PlayerArray = new Token[numPlayers]; // Creates an array to store player objects

    for(int i = 1; i <= numPlayers; i++) // loop to for players to chose a token
    {
        System.out.println("Enter (1) for Battle Ship");
        System.out.println("Enter (2) for Top Hat");
        System.out.println("Enter (3) for Dog");
        System.out.println("Enter (4) for Boot");
        System.out.println("Enter (5) for Wheel Barrow");
        System.out.println("Enter (6) for Race Car");
        System.out.println("Enter (7) for Thimble");
        System.out.println("Enter (8) for Cat");

            System.out.println();
            System.out.println("Player " + i + " Pick a token: ");
            int userChoice = scan.nextInt();

            if(userChoice == 1)
            {
                System.out.println("You Picked: " + a.toString()); // 
                PlayerArray[i-1] = a;
            }

            else if(userChoice ==2)
            {
                System.out.println("You Picked: " + b.toString());
                PlayerArray[i-1] = b;
            }

            else if(userChoice == 3)
            {
                System.out.println("You Picked: " + c.toString());
                PlayerArray[i-1] = c;
            }

            else if(userChoice == 4)
            {
                System.out.println("You Picked: " + d.toString());
                PlayerArray[i-1] = d;
            }

            else if(userChoice == 5)
            {
                System.out.println("You Picked: " + e.toString());
                PlayerArray[i-1] = e;
            }

            else if(userChoice == 6)
            {
                System.out.println("You Picked: " + f.toString());
                PlayerArray[i-1] = f;
            }

            else if(userChoice == 7)
            {
                System.out.println("You Picked: " + g.toString());
                PlayerArray[i-1] = g;
            }

            else if(userChoice == 8)
            {
                System.out.println("You Picked: " + h.toString());
                PlayerArray[i-1] = h;
            }

            System.out.println();

    }

我解决了自己的谜语......

此解决方案仅根据玩家数量制作一个大小的数组,然后将玩家存储到数组中。我首先创建了对象,然后当用户选择对象时将其存储到数组中。为对象分配一个值供用户选择。然后,我可以从我在类中创建的方法中检索属于该用户的值。

我想发布此跟进,万一有人可能有一天会遇到与我相同的问题。