【发布时间】:2016-04-16 23:20:09
【问题描述】:
播放方法。这种方法应该允许我输入命令来旋转 4x4 数组的特定行或列。它需要在一个循环中(我假设while)总是要求一个命令,做旋转,打印它并在回到原来的时候结束,我在while循环中应该有什么问题,我有一个默认变量和一个当前变量,我两次都使用拼图,所以默认值被覆盖(这是错误的)所以循环不运行,我真的不知道如何解决这个问题,我知道它没有两次都需要困惑,但我保留它只是为了展示。
导入 java.util.Scanner;
公开课谜题{
public static final int N = 4;
public static final int NUMBER_OF_ROTATIONS = 5;
public static void main(String[] args) {
int[][] puzzle = new int[N][N];
reset(puzzle);
test(puzzle);
reset(puzzle);
scramble(puzzle);
System.out.println("### Testing puzzle game play\n");
play(puzzle);
}
public static void print(int[][] puzzle) {
for (int[] row : puzzle) {
for (int elem : row) {
System.out.printf("%4d", elem);
}
System.out.println();
}
System.out.println();
}
public static void test(int[][] puzzle) {
System.out.println("### Testing reset method\n");
print(puzzle);
System.out.println("### Testing rotate methods\n");
print(puzzle);
for (int i = 0; i < N; i++) {
System.out.println("### rotateColumn(" + i + ")\n");
rotateColumn(puzzle, i);
print(puzzle);
System.out.println("### rotateRow(" + i + ")\n");
rotateRow(puzzle, i);
print(puzzle);
}
reset(puzzle);
System.out.println("### Testing random rotations\n");
print(puzzle);
for (int i = 0; i < 5; i++) {
randomRotation(puzzle);
print(puzzle);
}
}
public static int[][] reset(int[][] puzzle) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
puzzle[i][j] = i * N + j;
}
return puzzle;
}
public static void scramble(int[][] puzzle) {
for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
randomRotation(puzzle);
}
}
// TODO: Implement method as specified in assignment brief
public static void rotateRow(int[][] arr, int row) {
int newRow = arr[row][arr.length - 1];
int nextRow;
for (int IndexNo = 0; IndexNo < arr.length; IndexNo++) {
nextRow = arr[row][IndexNo];
arr[row][IndexNo] = newRow;
newRow = nextRow;
}
}
// TODO: Implement method as specified in assignment brief
public static void rotateColumn(int[][] arr, int column) {
int newCol = arr[arr.length - 1][column];
int nextCol;
for (int IndexNo = 0; IndexNo < arr.length; IndexNo++) {
nextCol = arr[IndexNo][column];
arr[IndexNo][column] = newCol;
newCol = nextCol;
}
}
// TODO: Implement method as specified in assignment brief
public static void randomRotation(int[][] puzzle) {
int rowrandom = (int) (Math.random() * 3 + 1);
int colrandom = (int) (Math.random() * 3 + 1);
int option = (int) (Math.random() * 2 + 1);
if (option == 1) {
rotateRow(puzzle, rowrandom);
} else {
rotateColumn(puzzle, colrandom);
}
}
// TODO: Implement method as specified in assignment brief
static void play(int[][] puzzle) {
reset(puzzle);
int[][] Default = puzzle;
print(puzzle);
for (int i = 0; i < 5; i++) {
randomRotation(puzzle);
}
int[][] Current = puzzle;
print(puzzle);
while (Current!=Default) {
System.out.println("enter row x or col x: ");
Scanner input = new Scanner(System.in);
String x = input.nextLine();
if (x.equals("row 0")) {
rotateRow(puzzle, 0);
print(puzzle);
}
if (x.equals("row 1")) {
rotateRow(puzzle, 1);
print(puzzle);
}
if (x.equals("row 2")) {
rotateRow(puzzle, 2);
print(puzzle);
}
if (x.equals("row 3")) {
rotateRow(puzzle, 3);
print(puzzle);
}
if (x.equals("col 0")) {
rotateColumn(puzzle, 0);
print(puzzle);
}
if (x.equals("col 1")) {
rotateColumn(puzzle, 1);
print(puzzle);
}
if (x.equals("col 2")) {
rotateColumn(puzzle, 2);
print(puzzle);
}
if (x.equals("col 3")) {
rotateColumn(puzzle, 3);
print(puzzle);
}
}
}
}
【问题讨论】:
标签: java arrays loops while-loop