【问题标题】:while loop trouble with array in javajava中数组的while循环问题
【发布时间】: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


    【解决方案1】:

    您没有更改 int[][] 拼图的内容,您只是将其设置为您最初传递给该方法的相同数组。

    如果

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

    如果要更改拼图的值,您应该让该方法返回一个新数组并将其保存为 play 方法的 for 循环中的当前值。

    newPuzzle = randomRotation(puzzle);
    

    如上。

    【讨论】:

    • 我无法将 randomRotation 分配为变量
    • @sb33 因为 randomRotation 没有返回任何东西。您可以更改返回数组的方法
    • @viviboo3 这些方法是模板代码,我相信我们无法更改它
    • @sb33 它说:// TODO: Implement method as specified in assignment brief 所以我不明白为什么不
    • @viviboo3 不会弄乱以前的使用方式吗?
    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2016-09-10
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多