写的一个数组小程序


实现以下形式的输出

[1, 2, 3, 4, 5, 6]

[20, 21, 22, 23, 24, 7]

[19, 32, 33, 34, 25, 8]

[18, 31, 36, 35, 26, 9]

[17, 30, 29, 28, 27, 10]

[16, 15, 14, 13, 12, 11]]

源代码:

import java.util.*;


public class A {
	static int numMove = 1;
	static int x = 0;
	static int y = 0;
	static int[][] a = new int[6][6];
	//初始化
	public static void f(){
		int i,j;
		for(i=0;i<6;i++){
			for(j=0;j<6;j++){
				a[i][j] = 0;
			}
		}
	}

	private static void moveForward() {
		// TODO Auto-generated method stub
		if(numMove==36){
			a[x][y] = 36;
			int i;
			for(i=0;i<6;i++){
			System.out.println(Arrays.toString(a[i]));
			}
			System.exit(0);
		}
		if((y==5)){
			moveDown();
		}
		if (a[x][y+1]!=0) {
			moveDown();
		}
		
		a[x][y] = (numMove);
		System.out.println(x + "  " + y+"  "+numMove);
		y = y+1;
	
		numMove=numMove+1;

		moveForward();
		
	}
	private static void moveDown() {
		
		// TODO Auto-generated method stub
		if(numMove==36){
			a[x][y] = 36;
			int i;
			for(i=0;i<6;i++){
				System.out.println(Arrays.toString(a[i]));
			}
			System.exit(0);
		}
		if((x==5)){
			moveBack();
		}
		if (a[x+1][y]!=0) {
			moveBack();
		}
	
		a[x][y] = (numMove);
		System.out.println(x + "  " + y+"  "+numMove);
		x = x+1;
	
		numMove=numMove+1;

		moveDown();
	}
	private static void moveBack() {
		// TODO Auto-generated method stub
		if(numMove==36){
			a[x][y] = 36;
			int i;
			for(i=0;i<6;i++){
				System.out.println(Arrays.toString(a[i]));
			}
			System.exit(0);
		}
		if((y==0)){
			moveUp();
		}
		if (a[x][y-1]!=0) {
			moveUp();
		}
	
		a[x][y] = (numMove); 
		System.out.println(x + "  " + y+"  "+numMove);
		y = y-1;
	
		numMove=numMove+1;

		moveBack();
	}
	private static void moveUp() {
		// TODO Auto-generated method stub
		if(numMove==36){
			a[x][y] = 36;
			int i;
			for(i=0;i<6;i++){
				System.out.println(Arrays.toString(a[i]));
			}
			System.exit(0);
		}
		if((x==0)){
			moveForward();
		}
		if (a[x-1][y]!=0) {
			moveForward();
		}
	
		a[x][y] = (numMove); 
		System.out.println(x + "  " + y+"  "+numMove);
		x = x-1;

		numMove=numMove+1;

		moveUp();
	}



	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		f();
		moveForward();
		
		
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值