首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“玩”是我拼图中的一部分不回任何东西

“玩”是我拼图中的一部分不回任何东西
EN

Stack Overflow用户
提问于 2016-04-16 18:55:03
回答 2查看 92关注 0票数 1

游戏的一部分我的拼图没有返回任何东西,我使用了类似的代码在其他领域的工作。我没有错误,所以我不知道出了什么问题,我试着使用'==‘并分别输出谜题,但同样没有什么事情发生。任何帮助都是有用的。益智也应该使用一个循环,游戏应该结束时,谜题解决了,但我不知道使用什么作为前后,任何帮助在这里也是有用的。

代码语言:javascript
复制
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);

        }




    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-16 19:05:39

对于未显示任何内容的谜题,Scanner只在使用input.next()方法时才会读取第一个单词。要读取带有空格的文本(例如“第1行”),需要使用input.nextLine()

票数 2
EN

Stack Overflow用户

发布于 2016-04-16 19:04:40

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

您应该将该方法声明为:

代码语言:javascript
复制
static int[][] play(int[][] puzzle) {
    ...
    return puzzle;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36668342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档