Java 使用数组打印蛇形图案
我在一项任务中遇到问题,要求我们打印此数组:Java 使用数组打印蛇形图案,java,arrays,multidimensional-array,Java,Arrays,Multidimensional 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 我的输出: Choose a number for the rows from 0 to 16. 5 Choose a number for the columns from 0 to 16 5 1 0 10 0 19 2 9 11 18 20
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
我的输出:
Choose a number for the rows from 0 to 16.
5
Choose a number for the columns from 0 to 16
5
1 0 10 0 19
2 9 11 18 20
3 8 12 17 21
4 7 13 16 22
5 6 14 15 23
我的代码:
//蛇随数字移动
导入java.util.Scanner;
公共级蛇行{
公共静态void main(字符串[]args){
//创建扫描仪对象
扫描仪inScan=新扫描仪(System.in);
//提示用户为0到16之间的行选择数字
System.out.println(“为0到16之间的行选择一个数字”);
//使用nextInt()方法从用户处获取输入
//使用变量int行
int row=inScan.nextInt();
//提示用户为0到16之间的列选择数字
System.out.println(“为0到16列选择一个数字”);
//使用nextInt()从用户处获取输入
//使用变量int col
int col=inScan.nextInt();
如果(行!=列){
System.out.println(“再次运行程序并为行和列选择相同的数字”);
系统出口(0);
}
int[]arr=move(行、列);
对于(int i=0;i0;i--){
//指定count的增量值
//到特定阵列单元
数组[i][j]=计数;
计数++;
}
flag=true;
}
}//列增量
返回数组;
}//移动方法
}//尾蛇类
任何人都能检测出导致错误的原因吗?任何帮助都将不胜感激。这将生成您描述的“蛇行”模式
它可以简化为三元,但我认为这使它更可读
这将是有趣的,找到一个更聪明的方式,但如果有人找到一个更好的方式请评论
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;
}
publicstaticint[]genArray(int-length){
int[]arr=新的int[length][length];
int计数器=0;
for(int col=0;col=0;row--){
System.out.println(“行:+row+”,列:+col);
arr[row][col]=计数器++;
}
}
}
}
返回arr;
}
这将生成您描述的“蛇行”模式
它可以简化为三元,但我认为这使它更可读
这将是有趣的,找到一个更聪明的方式,但如果有人找到一个更好的方式请评论
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;
}
publicstaticint[]genArray(int-length){
int[]arr=新的int[length][length];
int计数器=0;
for(int col=0;col=0;row--){
System.out.println(“行:+row+”,列:+col);
arr[row][col]=计数器++;
}
}
}
}
返回arr;
}
您可以创建如下数组:
intm=5;
int n=4;
int[]snakeArr=IntStream.range(0,n)
.mapToObj(i->IntStream.range(0,m)
//平直排列顺序,
//奇数行-逆序
.map(j->(i%2==0?j:m-j-1)+i*m+1)
.toArray())
.toArray(int[]]::新建);
//输出
数组.流(蛇形鱼)
.map(行->数组.stream(行)
.mapToObj(e->String.format(“%2d”,e))
.collect(收集器.连接(“”))
.forEach(System.out::println);
转置snake数组:
int[]transposedSA=新的int[m][n];
IntStream.range(0,m).forEach(i->
IntStream.range(0,n).forEach(j->
transposedSA[i][j]=蛇[j][i]);
//输出
Arrays.stream(transposedSA)
.map(行->数组.stream(行)
.mapToObj(e->String.format(“%2d”,e))
.collect(收集器.连接(“”))
.forEach(System.out::println);
您可以按如下方式创建这样的数组:
intm=5;
int n=4;
int[]snakeArr=IntStream.range(0,n)
.mapToObj(i->IntStream.range(0,m)
//平直排列顺序,
//奇数行-逆序
.map(j->(i%2==0?j:m-j-1)+i*m+1)
.toArray())
.toArray(int[]]::新建);
//输出
数组.流(蛇形鱼)
.map(行->数组.stream(行)
.mapToObj(e->String.format(“%2d”,e))
.collect(收集器.连接(“”))
.forEach(System.out::println);
转置snake数组:
int[]transposedSA=新的int[m][n];
IntStream.range(0,m).forEach(i->
IntStream.r
1 10 11 20
2 9 12 19
3 8 13 18
4 7 14 17
5 6 15 16
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
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
public class Main {
public static void main(String[] args) {
final int LEN = 5;
int[][] arr = new int[LEN][LEN];
int start = 1, j, k, col;
for (int i = 0; i < LEN; i++) {
if (i % 2 == 0) {
k = 1;
for (j = start, col = 0; k <= LEN; j++, k++, col++) {
arr[i][col] = j;
}
start = j - 1 + LEN;
} else {
k = 1;
for (j = start, col = 0; k <= LEN; j--, k++, col++) {
arr[i][col] = j;
}
start++;
}
}
// Print the transpose of the matrix
for (int r = 0; r < LEN; r++) {
for (int c = 0; c < LEN; c++) {
System.out.print(arr[c][r] + "\t");
}
System.out.println();
}
}
}
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
1 20 21 40 41 60 61 80 81 100
2 19 22 39 42 59 62 79 82 99
3 18 23 38 43 58 63 78 83 98
4 17 24 37 44 57 64 77 84 97
5 16 25 36 45 56 65 76 85 96
6 15 26 35 46 55 66 75 86 95
7 14 27 34 47 54 67 74 87 94
8 13 28 33 48 53 68 73 88 93
9 12 29 32 49 52 69 72 89 92
10 11 30 31 50 51 70 71 90 91
import java.util.Arrays;
import java.util.Scanner;
public class SnakePatternProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // *INPUT*
int row = scanner.nextInt();
int col = scanner.nextInt();
int[][] array = new int[row][col];
// Initializing first row of the array with a generalized expression
for (int i = 0; i < col; i++) {
if (i % 2 != 0) array[0][i] = col * (i+1);
else array[0][i] = (col * i) + 1;
}
array[0][0] = 1; // this element is not covered in the above loop.
// Nested loop for incrementing and decrementing as per the first row of elements.
for (int i= 1; i< row; i++){
for (int j=0; j< col; j++){
if(j%2==0) array[i][j] = array[i-1][j] + 1;
else array[i][j] = array[i-1][j] - 1;
}
}
System.out.println(Arrays.deepToString(array)); // *OUTPUT
}
}
1 10 11 20 21
The element in the odd column is equivalent to ((currentColumn + 1) * Total_No_Of_columns);
Example: arr[0][1] = (1 + 1)* 5 = 10
The element in the even column is equivalent to (currentColumn * Total_No_Of_columns) + 1;
Example: arra[0][2] = (2 * 5) + 1 = 11;
arr[0][0] = 1 -> must be declared
Elements with even column number gets incremented by 1 in each row from the first element of that column.
1 -> First element of column-0
2 -> (1 + 1)
3 -> (2 + 1).... so on
4
5
Elements with odd column number gets decremented by 1 in each row from the first element of that column.
10 -> First element of column - 1
9 -> (10 - 1)
8 -> (9 - 1).... so on
7
6