⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 circle.java

📁 回形数组 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
💻 JAVA
字号:
public class Circle{

	static int width =4,height= 4;
	static int matrix[][]; 
	static int pos =1;  //方向 1右2下3左4上

	public static void main(String[] args){
	System.out.println("\u66b4\u8d70\u7684\u60c5\u7eea");
	System.out.println("\u59d0\u59d0\u65b0\u751f\u6d3b\u7684\u5f00\u59cb\uff01\uff01");
	System.out.println("\u59d0\u59d0\u627e\u5230\u4e86\u4e00\u4e2a\u53ef\u4ee5\u4f9d\u9760\u4e00\u751f\u7684\u4eba\u4e86\uff01\uff01 ");
	System.out.println("\u6211\u7684\u5927\u5b66\u751f\u6d3b");
	System.out.println("\u670b\u53cb\u4eec\u548c\u6211\u5728\u6e05\u5927\u4e2d\u7684\u751f\u6d3b");

int a = 8,b = 4;

int expr = a++%++b*2;
System.out.println(expr);





		// 获得 matrix 属性 需要 try
		try{
			width = height = Integer.parseInt(args[0]);
		}catch(ArrayIndexOutOfBoundsException ex){
		}
		try{
			height = Integer.parseInt(args[1]);
		}catch(ArrayIndexOutOfBoundsException ex){
		}
		matrix = new int[height][width];
		
		for(int temp=1,i=0,j=0;temp<=width*height;temp++){
			matrix[i][j]=temp;
			//方向 1右2下3左4上
			// 只要判断按方向走下去的位置没有越界和值为0就可以
			// 否则判断顺时针方向上的位置
			if(pos ==1){
				if(j+1<width && matrix[i][j+1]==0){
					j++;
					pos=1;
				}else if(i+1<height && matrix[i+1][j]==0){
					i++;
					pos=2;
				}		
			} else if(pos ==2){
				if(i+1<height && matrix[i+1][j]==0){
					i++;
					pos=2;
				}else if(j-1>=0 && matrix[i][j-1]==0){
					j--;
					pos=3;
				}	
			} else if(pos ==3){
				if(j-1>=0 && matrix[i][j-1]==0){
					j--;
					pos=3;
				}else if(i-1>=0 && matrix[i-1][j]==0){
					i--;
					pos=4;
				}
			} else if(pos ==4){
				if(i-1>=0 && matrix[i-1][j]==0){
					i--;
					pos=4;
				}else if(j+1<width && matrix[i][j+1]==0){
					j++;
					pos=1;
				}	
			}
		}
		
		//输出
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				System.out.print("\t"+matrix[i][j]);	
			}
			System.out.println();
		}
	}
} //end of class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -