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
java-为什么我的代码会填充所有值_Java_Arrays - Fatal编程技术网

java-为什么我的代码会填充所有值

java-为什么我的代码会填充所有值,java,arrays,Java,Arrays,这是我的程序中的一种方法,在游戏中给玩家“初始手牌”,玩家数组是一个包含玩家对象的数组。玩家对象本身是数组或卡片对象,它们在其他类中定义 private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){ System.out.println( "\nPlease enter the name of the deck file" ); File theDeck = new

这是我的程序中的一种方法,在游戏中给玩家“初始手牌”,玩家数组是一个包含玩家对象的数组。玩家对象本身是数组或卡片对象,它们在其他类中定义

private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
   System.out.println( "\nPlease enter the name of the deck file" );
   File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
   int intLine;

   try{
       //fileReader = new BufferedReader ( new FileReader( theDeck ) );
       Scanner fileScanner = new Scanner( theDeck );

       int line = 0;

       for( int h = 0; h < noCardsInHand; h++){
           for( int p = 0; p < noPlayers; p++){
                System.out.println( "Player " + h + " Card" + p );

                line = Integer.parseInt( fileScanner.nextLine() );

                players[p].setHand( line, h );

           }
       }
       seePlayerHands();
   }catch (Exception e) {
       e.printStackTrace();
   }
}
但是当seePlayerHands()方法时,如果最后读取的卡是2,则会出现这种情况

 Player 1's hand is currently
 2
 2
 2
 Player 2's hand is currently
 2
 2
 2
 Player 3's hand is currently
 2
 2
 2
而不是这样

 Player 1's hand is currently
 4
 2
 5
 Player 2's hand is currently
 3
 2
 2
 Player 3's hand is currently
 5
 5
 2
从看起来像

 4
 3
 5
 2
 2
 5
 5
 2
 2
 4
 4
 6
 3
 5
 4
 6
 2
 3
谢谢你的帮助

把所有东西都放在这里

  public class CardGame{
  static Player[] players;
  static int handSize;
  public static void main(String[] args){
    Scanner reader = new Scanner(System.in);

    System.out.println( "\nHello, how many players would you like" );
    int playersNum = Integer.parseInt(Checks.userInputCheck( "\\d" ));
    System.out.println( "\nHow many cards should each player begin with" );
    int handSize = Integer.parseInt(Checks.userInputCheck( "\\d" ));
    System.out.println( "\nWhich strategy would you like to use 1 or 2" );
    int strategy = Integer.parseInt(Checks.userInputCheck( "[12]$" ));

    Logger.createDeck( playersNum, handSize );

    makePlayers( playersNum, handSize, strategy );

    dealInitialHand( playersNum, players, handSize );

    makePlayerOutputs();

    for ( int i = 0; i < players.length; i++){
       logInitialHand(players[i]);
    }

    //for ( int i = 0; i < players.length; i++){
      //  new Thread(players[i]).start();
   // }
   }

    private static void makePlayers(  int noPlayers, int noCardsInHand, int strategyChosen){
   players = new Player[noPlayers];
   for( int i = 0; i < noPlayers; i++){
       players[i] = new Player( strategyChosen, noCardsInHand, i+1 );
       players[i].fillHand();
   }
}

   private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
   System.out.println( "\nPlease enter the name of the deck file" );
   File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
   //BufferedReader fileReader = null;
   int intLine;


   try{
       //fileReader = new BufferedReader ( new FileReader( theDeck ) );
       Scanner fileScanner = new Scanner( theDeck );

       int line = 0;

       for( int h = 0; h < noCardsInHand; h++){
           seePlayerHands();
           for( int p = 0; p < noPlayers; p++){
                System.out.println( "Player " + h + " Card" + p );

                line = Integer.parseInt( fileScanner.nextLine() );
                //seePlayerHands();
                players[p].setHand( line, h );
                //seePlayerHands();
           }
       }
   }catch (Exception e) {
       e.printStackTrace();
   }

   seePlayerHands();
    }

   private static void seePlayerHands(){
   for ( int i = 0; i < players.length; i++){
      System.out.println( players[i].getPlayerName() + "'s hand is currently" );
      players[i].seeHand();
    }
    }
这是罗斯建议的改变的结果

Player 1's hand is currently
0
0
0
Player 2's hand is currently
0
0
0
Player 3's hand is currently
0
0
0
Player 0 Card0value5
Player 1 Card0value3
Player 2 Card0value6
Player 1's hand is currently
6
6
6
Player 2's hand is currently
6
6
6
Player 3's hand is currently
6
6
6
Player 0 Card1value1
Player 1 Card1value3
Player 2 Card1value5
Player 1's hand is currently
5
5
5
Player 2's hand is currently
5
5
5
Player 3's hand is currently
5
5
5
Player 0 Card2value3
Player 1 Card2value3
Player 2 Card2value2
Player 1's hand is currently
2
2
2
Player 2's hand is currently
2
2
2
Player 3's hand is currently
2
2
2

感谢您的快速回复,希望这能帮助您帮助我。

在您的
fillHand
方法中,您将手中的每张卡都设置为
卡的同一实例:

public void fillHand(){
    Card card = new Card(0);

    for ( int i = 0; i < hand.length; i++){
        hand[i] = card;
    }
}
应该没问题

编辑
正如peeskillet指出的,
cardValue
不应该是静态的。

cardValue
是静态的。这意味着所有卡都将收到相同的值

static int cardValue;
移除
静态

而且,甚至在你用更多的代码编辑你的帖子之前,我就在考虑你引用同一个卡片对象的可能性。如果这对你不完全有效,我也会改变@sstefff的建议

public void fillHand(){
    for ( int i = 0; i < hand.length; i++){
        hand[i] = new Card(0);
    }
}
public void fillHand(){
对于(int i=0;i
向我们展示您的玩家等级和seePlayersHand方法;)首先初始化玩家的代码:)这不是错误,而是
System.out.println(“玩家”+h+“卡”+p)应该是
System.out.println(“玩家”+p+“卡”+h)…可能也值得做
System.out.println(“玩家”+p+“卡”+h+“值”+行)并将其下移到您的行下方assignment@Fortunato不需要写,如果你同意的话,只需更新他的评论:)我觉得这可能是问题所在,因为我的代码用于在最初的hand方法读取行后创建card对象,然后使用该值制作卡片。我做了打印输出,得到了相同的结果,这是在setHand方法完成之前,所以数组在对数组进行setHand之前都有值。我做了更改,但没有更改任何内容问题仍然存在:(peeskillet是正确的,我没有抓住这一点。将peeskillet的答案和我的答案结合起来,告诉我们这样做是否更好。@user3074140因为您是StackOverflow的新手,您可能不知道,除了说“谢谢”之外,您还应该回答解决问题中描述的问题。哦,是的,忘了这么做。谢谢
public void fillHand(){
    Card card = new Card(0);

    for ( int i = 0; i < hand.length; i++){
        hand[i] = card;
    }
}
public void fillHand(){
    for ( int i = 0; i < hand.length; i++){
        hand[i] = new Card(0);
    }
}
static int cardValue;
public void fillHand(){
    for ( int i = 0; i < hand.length; i++){
        hand[i] = new Card(0);
    }
}