Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归骑士位置给定移动次数字符串数组Java_Java_Arrays_Recursion_Row_Chess - Fatal编程技术网

递归骑士位置给定移动次数字符串数组Java

递归骑士位置给定移动次数字符串数组Java,java,arrays,recursion,row,chess,Java,Arrays,Recursion,Row,Chess,我试图打印一个板,在给定骑士初始位置的情况下,它显示8x8板上在指定移动次数内可以到达的所有位置。我想到的输入和输出示例如下: Number of Moves:1 Initial Row:3 Initial Column: 3 ........ ..x.x... .x...x.. ...x.... .x...x.. ..x.x... ........ ........ 我不断地发现这个错误: at KnightMoves.move(KnightMoves.java:24) at Knight

我试图打印一个板,在给定骑士初始位置的情况下,它显示8x8板上在指定移动次数内可以到达的所有位置。我想到的输入和输出示例如下:

Number of Moves:1
Initial Row:3
Initial Column: 3

........
..x.x...
.x...x..
...x....
.x...x..
..x.x...
........
........
我不断地发现这个错误:

at KnightMoves.move(KnightMoves.java:24)
at KnightMoves.move(KnightMoves.java:27)
这是我的解决方案:

import java.util.Scanner;
public class KnightMoves{
    public static void printBoard(String array [][]){
        for (int row=0; row<8; row++){
            for (int col=0; col<8; col++){
                    array [row][col]=".";
            }
        }
    }
    public static void initializeArray(String array [][]){
        for (int row=0; row<8; row++){
            for (int col=0; col<8; c++){
                array [row][col]=".";
            }
        }
    }
    public static void move (String array [][], int steps, int row, int col){
        if (steps==0){
            System.exit(0);
        }else{
            if (row<0 || row>=8 || col<0 || col>=8){
                return;
            }
            move(array,steps,row-2,col-1);
            move(array,steps,row-2,col+1);
            move(array,steps,row+2,col-1);
            move(array,steps,row+2,col+1);
            move(array,stepst,row-1,col-2);
            move(array,steps,row-1,col+2);
            move(array,steps,row+1,col-2);
            move(array,steps,row+1,col+2);
            steps=steps-1;
         }
     }
    public static void main (String args[]){
        Scanner s=new Scanner (System.in);
        String array [][]=new String [8][8];
        initializeArray(array);
        System.out.println("Number of moves:");
        int steps=s.nextInt();
        System.out.println("Starting row:");
        int row=s.nextInt();
        System.out.println("Starting column:");
        int col=s.nextInt();
        move(array,steps,row,col);
        printBoard(array);
    }
 }
import java.util.Scanner;
公开级骑士招式{
公共静态void打印板(字符串数组[]){

对于(int row=0;row第一次尝试很好。您的代码存在一些问题:

在move方法中,步骤的退出条件应该像对无效行/列那样简单地返回。您还需要在骑士移动到的位置存储一个“X”。最后,在调用递归时立即减少步骤变量

这些变化如下所示:

public static void move(String array[][], int steps, int row, int col) {
    if (steps < 0 || row < 0 || row >= 8 || col < 0 || col >= 8)
        return;
    array[row][col] = "X";
    move(array, steps - 1, row - 2, col - 1);
    move(array, steps - 1, row - 2, col + 1);
    move(array, steps - 1, row + 2, col - 1);
    move(array, steps - 1, row + 2, col + 1);
    move(array, steps - 1, row - 1, col - 2);
    move(array, steps - 1, row - 1, col + 2);
    move(array, steps - 1, row + 1, col - 2);
    move(array, steps - 1, row + 1, col + 2);
}

在此之后,您将获得预期的输出。

for(int col=0;col非常感谢!不知怎的,这给了我一个错误:需要类、接口或枚举。这是否意味着我应该将它们分成四个文件?所有内容都在KnightMoves类中。这两个方法应该替换原来同名的两个方法。我没有重新发布任何未更改的代码。我添加了一个额外的括号by事故:)非常感谢。这很有帮助。我会尝试练习一些其他问题,这样我可以提高!再次感谢你。
public static void printBoard(String array[][]) {
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            System.out.print(array[row][col]);
        }
        System.out.println();
    }
}