做个回环数 显示的一个算法,,大家帮看看哪里错了 谢谢哈
- package cn.itheima;
- public class RollNumber {
- public static void main(String[] args) {
- rollNumPrint(5,5);
- }
-
- public static void rollNumPrint(int x,int y)
- {
- x--;y--;
- int[][] arr= new int[x][y];
- //flag means array get number's direction 1RT/2DN/3LT/4UP/0STOP
- int flag = 1;
- //record roll times
- int roll = 0;
- //cnt is the counter add itself.
- int cnt = 1;
- int ax = 0;
- int ay = 0;
-
- while(true)
- {
- arr[ax][ay] = cnt;
- cnt++;
- if(cnt == (x+1)*(y+1)+1)break;
- switch (flag)
- {
- case 1:
- if(ax < x-roll)ax++;
- else
- {
- flag++;
- continue;
- }
- break;
- case 2:
- if(ay < y-roll)ay++;
- else{
- flag++;
- continue;
- }
- break;
- case 3:
- if(ax > roll)ax--;
- else {
- flag++;
- roll++;
- continue;
- }break;
- case 4:
- if(ay > roll)ay--;
- else
- {
- flag = 1;
- continue;
- }
- break;
- default:
- System.out.println("Error!");
- break;
- }
- }
- for(int tmp=0 ; tmp <= y; y++)
- {
- for(int tmpx=0 ; tmpx<= x; x++)
- {
- System.out.print(arr[tmpx][tmp]+'\t');
- }
- System.out.println("");
- }
- }
- }
复制代码 |