2014-04-09 38 views
-2

我很难尝试在我的Java程序上实现此功能。有什么建议么? 问:初始化所有元素,除了最后一行,并随机生成0或1 最后一列且我们必须把这个魔术类二维数组有助于使用java

/** 
* 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 
    } 
} 
+0

要具体明确下。问题是什么? –

+1

这不是家庭作业溢出 – Coderchu

+0

你为什么要创建两个数组?你在for循环(good)之外构造网格,然后在for循环(坏)中构造矩阵。 – user924272

回答

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

}