二维数组有助于使用java
我很难在java程序中实现这一点。有什么建议吗? Q:将除最后一行和最后一列之外的所有元素初始化为随机生成的0或1 我们必须把它放在魔术课上二维数组有助于使用java,java,arrays,Java,Arrays,我很难在java程序中实现这一点。有什么建议吗? Q:将除最后一行和最后一列之外的所有元素初始化为随机生成的0或1 我们必须把它放在魔术课上 /** * Service class with supporting methods for Magic Trick * * * @version 4/7/14 */ import java.util.*; import java.text.*; public class MagicTrick { private int[][] gr
/**
* 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
}
}
/**
*具有魔术支持方法的服务类
*
*
*@version 4/7/14
*/
导入java.util.*;
导入java.text.*;
公共级MagicTrick
{
私有int[][]网格;
私有int flippedRow;
私有int-flippedCol;
公共静态最终整数网格大小=6;
//int[][]矩阵=新int[flippedRow][flippedCol];
//Random rand=新的Random();
/**
*默认构造函数,
*将网格中的元素设置为随机生成0或1
*调用setParity方法
*/
公共MagicTrick()
{
this.grid=newint[grid_SIZE][grid_SIZE];
for(int r=0;r
public MagicTrick()
{
//见Lab9a注释
this.grid=newint[grid_SIZE][grid_SIZE];
随机通配符=新随机();
int start=0,end=2;
for(int r=0;rBe特定且明确。问题是什么?这不是HomeWorkoverFlow为什么要创建两个数组?在for循环外构造网格(好),然后在for循环内构造矩阵(坏).我正在尝试让我的5x5矩阵完全随机化0和1我不知道如何使用布尔语句我不确定我是否正确地这样做,如何才能让它完美地工作布尔[][]矩阵=新布尔[r][c];随机兰德=新随机();for(int I=0;I<5;I++)for(int j=0;j<5;j++)矩阵[r][c]=rand.nextBoolean();返回矩阵[];
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();
}