有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我谜题的java“玩”部分不会返回任何东西

我的拼图游戏部分没有返回任何内容,我在其他领域使用过类似的代码。我没有得到错误,所以我不知道是什么错误,我尝试使用“==”并单独输出拼图,但再次没有发生任何事情。任何帮助都是有用的。谜题也应该使用循环,游戏应该在谜题解决后结束,但我不确定使用什么作为之前和之后,这里的任何帮助也会有用

package assignment;

import java.util.Scanner;

import static java.util.Scanner.*;


public class puzzle {

    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 void reset(int[][] puzzle) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                puzzle[i][j] = i * N + j;
        }
    }

    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);
        print(puzzle);

        for (int i = 0; i < 5; i++) {
            randomRotation(puzzle);
        }
        print(puzzle);


        System.out.println("enter row x or col x: ");
        Scanner input = new Scanner(System.in);
        String x = input.next();

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

        }




    }
}

共 (2) 个答案

  1. # 1 楼答案

    您的play方法不会返回任何内容,因为它被声明为void,实际上您甚至没有返回语句

    您应该将该方法声明为:

    static int[][] play(int[][] puzzle) {
        ...
        return puzzle;
    }
    
  2. # 2 楼答案

    对于没有显示任何内容的拼图,如果使用Scanner方法,则input.next()只会读取第一个单词。要阅读带有空格的文本(例如“row 1”),需要使用input.nextLine()