In the Java language, I don't know how to write the final output statement of the spiral matrix by inputting the length and width

CSDN Q & A 2022-01-15 02:36:08

package zuoye;

import java.util.Scanner;

public class juzhen {
private static Scanner s;
private static char[][][] nums;

public static void main(String[] args){ s = new Scanner(; System.out.print(" Please enter the length of the matrix :"); int indexC=s.nextInt(); System.out.print(" Please enter the width of the matrix :"); int indexK=s.nextInt(); if(indexC<=0||indexK<=0){ System.out.println(" The number you entered is illegal !"); return; } int[][] square=new int[indexC][indexK]; int x=0;int y=0; for(int i=1;i<=indexC*indexK;){ while(y<square[x].length-1&&square[x][y+1]==0){ square[x][y++]=i++; } while(x<square.length&&square[x][y]==0){ square[x++][y]=i++; } while(y>0&&square[x-1][y-1]==0){ square[x-1][--y]=i++; } if(++y>) break; } System.out.println(nums[x][y]);}}

Similar articles