簡體   English   中英

二維數組幫助使用java

[英]Two-dimensional arrays help using java

我很難在我的 Java 程序中實現它。 有什么建議? 問:初始化除最后一行和最后一列之外的所有元素隨機生成 0 或 1 我們必須將其放在 Magic Trick 類下

/**
 * Service class with supporting methods for Magic Trick
 *
 * 
 * @version 4/7/14
 */

import java.util.*;
import java.text.*;

public class MagicTrick
{
  private int[][] grid;
  private int flippedRow;
  private int flippedCol;
  public static final int GRID_SIZE = 6;
  //int [][] matrix = new int[flippedRow][flippedCol];

  //Random rand = new Random();
  /**
   * default constructor,
   * sets elements in the grid to randomly generated either 0 or 1
   * calls setParity method
   */
  public MagicTrick()
  {
    this.grid = new int [GRID_SIZE][GRID_SIZE];
    for ( int r = 0; r < this.grid.length - 1; r++ )
    {
      for ( int c = 0; c < this.grid [r].length - 1; c++ )
      {

          boolean [][] matrix = new boolean[r][c];
          Random rand = new Random();
          for( int i = 0; i < 5; i++ )
          for ( int j = 0; j < 5; j++ )
          matrix[r][c] = rand.nextBoolean();
          return matrix[][];
          //nextBoolean = true;
        //{
        //Need to add nextBoolean and make a boolean statement first in order to continue
        //}
        this.grid[r][c] = 1;
      }

      //if( c < this.grid[r].length)
      //{
      //  return c;
      //}
    }
    setParity();
    // see Lab9a Notes

  }

  /**
   * sets elements in the last row and the last column
   * to either 1 or 0, so the sum of the elements in the appropriate row and column is even
   *
   */
  public void setParity()
  {

    //for(int r = 0; r; r++)
    //{
    // for(int c = 0; c; c++)
    //{

    //}
    // }
    // See Lab9a Notes



  }

  /**
   * flips the value of the randomly
   * selected element
   */
  public void flipOneElement()
  {

    // See Lab9a Notes

  }

  /**
   * accessor method
   * @return  the value of the row of the flipped element: this.flippedRow
   */
  public int getFlippedRow()
  {
    return flippedRow;
  }

  /**
   * accessor method
   * @return  the value of the column of the flipped element: this.flippedCol
   */
  public int getFlippedColumn()
  {

    return flippedCol;
  }

  /**
   * toString method returns printable version
   * of the content of this.grid
   */
  public String toString()
  {
    String returnValue = "";
    for (int r = 0; r < GRID_SIZE; r++)
    {
      for (int c = 0; c < GRID_SIZE; c++)
      {
        returnValue += this.grid[r][c] + " ";
      }
      returnValue += "\n";
    }
    return returnValue += "\n";
  }


  /**
   * scheck the users guess
   * 
   * @param r - the row selected by the user
   * @param c - the column selected by the user
   * @return - true if the guessed row and column are the same
   *                as the row and column of the flipped element
   */
  public boolean checkGuess(int r, int c)
  {
    return false;  // THIS IS A STUB
  }
}
public MagicTrick()
{
  // see Lab9a Notes
    this.grid = new int [GRID_SIZE] [GRID_SIZE];

    Random wildCard = new Random();
    int start=0, end=2;
    for(int r=0; r<this.grid.length; r++)
    {
        for (int c=0; c<this.grid[r].length; c++)
        {
            this.grid[r][c]=wildCard.nextInt(end-start);
        }
    }
    setParity();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM