【问题标题】:Array will not update but rather print out the wrong array数组不会更新而是打印出错误的数组
【发布时间】:2019-05-16 02:28:26
【问题描述】:

我设法弄清楚如何为我的连接四程序打印数组,但我无法用我的代码更新电路板,我查看并运行它,代码理论上可以工作,但是数组不会占用新的输入

我尝试使用 for 循环运行它,但结果是错误的,我正在考虑将 drop 方法放入 print board 方法中,但我觉得这会导致错误

public class Connect4 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // DON'T MODIFY THE MAIN METHOD UNLESS FOR DEBUGGING

 //MAKE SURE YOU GET RID OF YOUR MODIFICATIONS HERE BEFORE  

SUBMISSION

String[][] board = createEmptyBoard();

Scanner input = new Scanner(System.in);

boolean bl = true;

printPattern(board);

while(bl) {

    int player1 = 1 , player2 = 2 ,  userInput;



 System.out.println("Please drop a RED disk at the column between 0 

and 6:");
    userInput = input.nextInt();
    dropDisk(board, userInput , player1);
    printPattern(board);

   System.out.println("Please drop a YELLOW disk at the column  

between 0 and 6:");
   userInput  = input.nextInt();
   dropDisk(board, userInput , player2);
   printPattern(board);  


    String win = checkWinner(board);
    /*
    Write code to announce  if there is  winner and end the game
    */


} 
 }
 public static String[][] createEmptyBoard() {
   /* This method prints the first empty pattern for the game
   DON'T MODIFY THIS METHOD
   */

    String[][] f = new String[7][15];
    for (int i =0;i<f.length;i++) {
          for (int j =0;j<f[i].length;j++) {

             if (j% 2 == 0) f[i][j] ="|";
             else f[i][j] = " ";
             if (i==6) f[i][j]= "-";
         }
       }
    return f;

} // end of createEmptyBoard 


  public static void printPattern(String[][] brd) {
  for (int i = 0; i < 7; i++){

System.out.println(brd[i][0] + brd[i][1]+ brd[i][2]+ brd[i][3]+ 

brd[i][4]+ brd[i][5]+ brd[i][6]+ brd[i][7]+ brd[i][8]+ brd[i][9]+   

brd[i][10]+ brd[i][11]+ brd[i][12]+ brd[i][13]+ brd[i][14]);

  }

  } // end of printPattern

  public static void dropDisk(String[][] brd, int position, int   

  player) {

       if (player == 1){

            brd[6][position] = "R";

           if(brd[6][position] == "R"){

           brd[6][position]  =  brd[6 - 1][position];

       } 

       }

       else if (player == 2){

           brd[6][position] = "Y";

           if(brd[6][position] == "Y"){

           brd[6][position]  =  brd[6 - 1][position];

       }

       }
/*Write your code to drop the disk at the position the user entered 
   depending on which player*/ 

} // end of dropDisk

【问题讨论】:

    标签: java


    【解决方案1】:

    dropDisk 的逻辑好像还没说完。 它将brd[6][position] 设置为RY,然后立即将其设置为brd[5][position] 的当前值。

    这应该始终是null

    【讨论】:

      【解决方案2】:

      在 Java 中,对象按值传递给方法。这意味着当您将参数传递给函数时,JVM makes a copy of that object which can be modified in the method

      在这种情况下,当您将brd 传递给dropDisk 时,它会被复制,并且您对dropDisk 中的副本进行更改。但是一旦dropDisk 结束,该副本就会被丢弃。您的main 方法不会对电路板进行任何更改。这是因为你的板子是一个字符串数组,而字符串是不可变的,这意味着它们在实例化后无法更改。

      如果您希望通过 main 方法更新板,请考虑在 dropDisk 中返回 brd

      【讨论】:

      • 可能是因为Strings 是不可变的
      • @ScaryWombat 我认为它通常也不适用于其他数据类型
      • 嗨 Nhami,你能告诉我这个问题“请在 0 到 6 之间的列放置一个 RED 磁盘:”的输入是什么?我的意思是我应该提供什么作为输入?此外,在 dropDisk(...) 方法中,您可能应该使用if (brd[6][position].equals("R"))if (brd[6][position].equals("Y")) 也是如此。不确定您对字符串数组的分配是否真的通过了。
      • @AlterV 你对原始数组是正确的,但是只要对象本身是可变的,对象数组就可以了
      • Java中对象不是按值传递的,对象引用是按值传递的!这是一个巨大的差异。如果您更新传递给方法的对象,调用者随后将看到更改。该方法将无法将参考点指向不同的对象。这意味着 brd[0][0]=“R” 将对调用者可见,但 brd=null 不可见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多