编程知识 cdmana.com

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

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.in);    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]);}}

版权声明
本文为[CSDN Q & A]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/12/20211207175245150s.html

Scroll to Top