Печать рисунка змеи в java с использованием массива - PullRequest
0 голосов
/ 11 апреля 2020

У меня проблемы с заданием, в котором мы должны распечатать этот массив:

1 10 11 20 21

2 9 12 19 22 * ​​1005 *

3 8 13 18 23

4 7 14 17 24

5 6 15 16 25

Мой код несколько верный, но он не печатает 10 и 19, как это должно быть.

Мой вывод:

Выберите число для строк от 0 до 16. 5 Выберите число для столбцов от 0 до 16 5 1 0 10 0 19
2 9 11 18 20
3 8 12 17 21
4 7 13 16 22 * ​​1020 * 5 6 14 15 23

Мой код:

//snake move with the number
import java.util.Scanner;

public class SnakeMove{

    public static void main(String[]args){

        //create Scanner object
        Scanner inScan = new Scanner (System.in);

        //prompt the user to choose number for the Row from 0 to 16
        System.out.println("Choose a number for the rows from 0 to 16.");

        //take the input from user with nextInt() method
        //use the variable int row
        int row = inScan.nextInt();

        //prompt the user to choose number for the Col from 0 to 16
        System.out.println("Choose a number for the columns from 0 to 16");

        //take the input from user with nextInt()
        //use the variable int col
        int col = inScan.nextInt();

        if (row != col){
            System.out.println("Run the program again and choose the same number for Row and Col");
            System.exit(0);
        }

        int [][] arr = move(row,col);

        for (int i = 0; i < arr.length; i++) {

            for (int j = 0; j < arr.length; j++) {

                System.out.print(arr[i][j] + "  ");
            }

            System.out.println();
        }

    }//main method

    static int [][] move(int row, int col){

        boolean flag = true;
        int count = 1;

        int [][] array = new int[row][col];

        for (int j = 0; j < array[0].length; j++){

            if (flag) {

                for (int i = 0; i < array.length; i++){
                    //assign the increment value of count
                    // to specific array cells
                    array[i][j] = count;
                    count++;

                }

                flag = false;

            }

            else {

                //row decrement going up
                for (int i = array.length-1; i > 0; i--) {
                    //assign the increment value of count
                    // to specific array cells
                    array[i][j] = count;
                    count++;
                }

                flag = true;

            }
        }//column increment

        return array;

    }//move method

}//end SnakeMove class

Может ли кто-нибудь обнаружить причину ошибка? Любая помощь будет оценена.

1 Ответ

0 голосов
/ 11 апреля 2020

Idk, если это именно то, что вы просите, но это сгенерирует шаблон "змеи", который вы описали.

Это может быть упрощено с помощью троичного кода, но это делает его более читабельным, я думаю

Было бы интересно найти более умный способ, если кто-то найдет лучший способ, пожалуйста, прокомментируйте

public static int[][] genArray(int length) {
    int[][] arr = new int[length][length];

    int counter = 0;
    for (int col = 0; col < arr.length; col++) {
        if (col % 2 == 0) {
            for (int row = 0; row < arr.length; row++) {
                arr[row][col] = counter++;
            }
        } else {
            for (int row = arr.length - 1; row >= 0; row--) {
                System.out.println("row: " + row + ", col: " + col);
                    arr[row][col] = counter++;
                }
            }
        }
    }
    return arr;
}
...