我很难在我的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
}
}
答案 0 :(得分: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();
}