-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
}
}
要具體明確下。問題是什麼? –
這不是家庭作業溢出 – Coderchu
你爲什麼要創建兩個數組?你在for循環(good)之外構造網格,然後在for循環(壞)中構造矩陣。 – user924272