2017-07-13 12 views
0

Arrayインデックスに格納されているTokenオブジェクトのString名を出力しようとしています。私が得るのは、おそらく場所アドレスのように見えるトークン@です。私はそれがそのオブジェクトの文字列を印刷したい。1次元配列に格納されたオブジェクトの特定の文字列を印刷する

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()); // 
     } 
    } 
} 
+0

私はそれを理解したと信じています...多分私はちょっとした睡眠が必要でした。 – CaptainChadL

答えて

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(); 

    } 

私は自分自身の謎を解く...

このソリューションは、選手だけの量にbassedサイズの配列を作成し、配列にプレーヤーを格納します。最初にオブジェクトを作成した後、そのオブジェクトが配列に格納されているユーザーによって選択されます。オブジェクトには、ユーザーが選択する値が割り当てられます。クラス内で作成したメソッドから、そのユーザーに属する値を取得できます。

誰かが私と同じ問題を抱えているような場合に備え、このフォローアップを投稿したかったのです。

関連する問題