Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 打印存储在一维数组中的对象的特定字符串_Arrays_Object - Fatal编程技术网

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

Arrays 打印存储在一维数组中的对象的特定字符串,arrays,object,Arrays,Object,我试图打印出存储在数组索引中的令牌对象的字符串名称。我所得到的是令牌@,看起来像是一个位置地址。我想让它打印那个对象的字符串 import java.util.Scanner; public class MainBoardDriver { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Squares

我试图打印出存储在数组索引中的令牌对象的字符串名称。我所得到的是令牌@,看起来像是一个位置地址。我想让它打印那个对象的字符串

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()); // 
        }
    }
}
import java.util.Scanner;
公共类主板驱动程序
{    
公共静态void main(字符串[]args)
{           
扫描仪扫描=新扫描仪(System.in);
Squares[]squaresarray=新的正方形[40];//创建一个数组来存储属性
squareArray[0]=新的GoSquare(“Gp”);
squareArray[1]=新地产广场(“地中海大道”,60,“紫色”);
squareArray[2]=新社区ChestSquares(“社区公益金”);
squareArray[3]=新地产广场(“波罗的海大道”,60,“紫色”);
squareArray[4]=新税种Squares(“所得税”,200);
squareArray[5]=新铁路广场(“阅读铁路”,200);
squareArray[6]=新地产广场(“东方大道”,100,“浅蓝色”);
squareArray[7]=新ChanceSquares(“机会”);
squareArray[8]=新地产广场(“佛蒙特大道”,100,“浅蓝色”);
squareArray[9]=新地产广场(“康涅狄格大道”,100,“浅蓝色”);
squareArray[10]=新的InjaiJustVisitingSquare(“在监狱中或刚刚访问”);
squareArray[11]=新地产广场(“St.Charles Place”,140,“粉色”);
squareArray[12]=新的公用事业广场(“电力公司”,150);
squareArray[13]=新地产广场(“州大道”,140,“粉色”);
squareArray[14]=新地产广场(“弗吉尼亚大道”,160,“粉色”);
squareArray[15]=新铁路广场(“宾夕法尼亚铁路”,200);
squareArray[16]=新地产广场(“圣詹姆斯广场”,180,“橙色”);
squareArray[17]=新社区ChestSquares(“社区公益金”);
squareArray[18]=新地产广场(“田纳西大道”,180,“橙色”);
squareArray[19]=新地产广场(“纽约大道”,200,“橙色”);
squareArray[20]=新的FreeParkingSquare(“免费停车”);
squareArray[21]=新地产广场(“肯塔基大道”,220,“红色”);
squareArray[22]=新ChanceSquares(“机会”);
squareArray[23]=新地产广场(“印第安纳大道”,220,“红色”);
squareArray[24]=新地产广场(“伊利诺伊大道”,240,“红色”);
squareArray[25]=新铁路广场(“B.和O.铁路”,200);
squareArray[26]=新地产广场(“大西洋大道”,260,“黄色”);
squareArray[27]=新地产广场(“Ventnor Avenue”,260,“黄色”);
squareArray[28]=新的实用广场(“水厂”,150);
squareArray[29]=新地产广场(“马文花园”,280,“黄色”);
squareArray[30]=新GoToJailSquare(“入狱”);
squareArray[31]=新地产广场(“太平洋大道”,300,“绿色”);
squareArray[32]=新地产广场(“北卡罗来纳大道”,300,“绿色”);
squareArray[33]=新社区ChestSquares(“社区公益金”);
squareArray[34]=新地产广场(“宾夕法尼亚大道”,320,“绿色”);
squareArray[35]=新铁路广场(“短线”,200);
squareArray[36]=新的ChanceSquares(“机会”);
squareArray[37]=新地产广场(“公园广场”,350,“蓝色”);
squareArray[38]=新税种Squares(“奢侈品税”,100);
squareArray[39]=新地产广场(“Boardwalk”,400,“蓝色”);
System.out.println(“输入玩家数量:”);
int numPlayers=scan.nextInt();
Token[]tokenArray=new Token[8];//创建一个数组来存储对象
令牌阵列[0]=新令牌(“战舰”,01500);
tokenArray[1]=新令牌(“Top Hat”,0,1500);
tokenArray[2]=新令牌(“狗”,0,1500);
tokenArray[3]=新令牌(“启动”,0,1500);
tokenArray[4]=新令牌(“手推车”,0,1500);
tokenArray[5]=新令牌(“赛车”,0,1500);
tokenArray[6]=新令牌(“顶针”,0,1500);
tokenArray[7]=新令牌(“Cat”,0,1500);
对于(inti=1;i
intnumplayers=0;
做
{
System.out.println(“输入2到6之间的玩家数量:”);
numPlayers=scan.nextInt();
}
而(数层<2 | |数层>6);
令牌a=新令牌(“战舰”,0,1500,0);
令牌b=新令牌(“顶帽”,0,1500,0);
令牌c=新令牌(“狗”,0,1500,0);
令牌d=新令牌(“启动”,0,1500,0);
代币e=新代币(“手推车”,0,1500,0);
代币f=新代币(“赛车”,0,1500,0);
令牌g=新令牌(“Thinble”,0,1500,0);
令牌h=新令牌(“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;
令牌[]玩家阵列=新令牌[numPlayers];//创建一个数组来存储玩家对象

对于(inti=1;我很好,所以我相信我已经弄明白了…也许我只是需要一点睡眠。
    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();

    }