使用数组打印蛇形图案 [英] Printing a snake pattern using an array
问题描述
我在分配我们需要打印该数组的作业时遇到麻烦:
I'm having trouble with an assignment where we are required to print out this array:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
我的代码在某种程度上是正确的,但它并未在应有的位置打印10和19.
My code is somewhat correct but it is not printing 10 and 19 where it should be.
我的输出:
为0到16的行选择一个数字.
Choose a number for the rows from 0 to 16.
5
为0到16的列选择一个数字
Choose a number for the columns from 0 to 16
5
1 0 10 0 19
1 0 10 0 19
2 9 11 18 20
2 9 11 18 20
3 8 12 17 21
3 8 12 17 21
4 7 13 16 22
4 7 13 16 22
5 6 14 15 23
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
任何人都可以检测到导致错误的原因吗?任何帮助将不胜感激.
Can anyone detect what is causing the error? Any help would be appreciated.
推荐答案
这将生成蛇行"消息.您描述的模式.
This will generate the "snaking" pattern you described.
可以用三元简化它,但是我认为这使其更具可读性
It could be simplified with ternary but this makes it more readable I think
但是,如果有人找到一种更好的方式发表评论,那么找到一种更聪明的方式将会很有趣
It would be interesting to find a more clever way about it though, if anyone finds a better way pls comment
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;
}
这篇关于使用数组打印蛇形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!