而在Java数组循环麻烦 [英] while loop trouble with array in java

查看:114
本文介绍了而在Java数组循环麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Play方法。此方法应允许我输入命令旋转的4×4阵列的特定行或列。它需要在一个循环(我假设而)总是问一个命令,做旋转,并将其打印和结束的时候回到原来的,我有什么我应该有作为while循环的麻烦,我有一个默认的和当前变量,我用拼图两次这样默认被覆盖(这是什么是错的),所以循环不会跑,我真的不知道该如何解决这个问题,我知道这不需要是困扰两次不过我在只是为了显示遵守。

Play method. this method should allow me to enter commands to rotate a certain row or column of a 4x4 array. it needs to be in a loop (i assume while) to always ask for a command, do the rotation, and print it and end when back to the original, I'm having trouble with what i should have as the while loop, i have a default and a current variable, i use puzzle both times so default gets overwritten (this is what is wrong) so the loop doesn't run, i don't really know how to get around this, i know it doesn't need to be puzzle both times however I've kept it in just to show.

进口java.util.Scanner中;

import 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);
        }


    }
}

}

推荐答案

你不改变INT的内容[] []拼图,你只是将其设置为你传递给方法最初在同一阵列

You're not changing the content of int[][] puzzle, you're just setting it to the same array you've passed to the method initially.

如果

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

应该改变拼图的值,你应该有方法返回一个新的数组并保存为当前内您的播放方法的循环。

should change the values of puzzle, you should have the method return a new array and save that as the current inside your for loop of the play method.

newPuzzle = randomRotation(puzzle);

和上面一样。

这篇关于而在Java数组循环麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆