提示用户输入行数、列数,打印蛇形数字。如下所示 1 2 3 4 8 7 6 5 9 10 11 12 ...

如题所述

刚编好的 完全符合你的要求 你看看可以不

import java.util.*;

public class Test {

public static void main(String[] args) {
System.out.println("输入列数");
int col = new Scanner(System.in).nextInt();
System.out.println("输入行数");
int row = new Scanner(System.in).nextInt();
for(int i=1; i<=row; i++) {
for(int j=1; j<=col; j++) {
if(i%2 == 1)
System.out.print(j + col * (i-1) +" ");
else
System.out.print(col - (j-1) + col * (i-1) +" ");
}
System.out.println("");
}

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-07
int i=0,j=1,n,t,m=1;
scanf("%d",&n);
t=n*n;
while(m<=t)
{if(j%2)
for(i=0;i<n;i++)
printf("%3d",m+i);
else
for(i=0;i<n;i++)
printf("%3d",m+n-i-1);
m+=n;
printf("\n");
j++;
}
第2个回答  2011-10-07
import java.util.Scanner;

public class SnakePrint {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Please input the row to genereate: ");
int row = scanner.nextInt();
System.out.print("Please input the column to generate: ");
int col = scanner.nextInt();

int number = row * col;

int i = 1;

boolean isRight = true;

while(i <= number){

if(isRight){
for(int j = 0; j < col; j++){
System.out.print(i++ + " ");
}

}else{
StringBuffer sb = new StringBuffer();
for(int j = col; j > 0; j--){
sb.insert(0, " "+i++);
}

System.out.print(sb.toString().trim());
}

System.out.println();
isRight = !isRight;
}

}

}
----------------testing
Please input the row to genereate: 3
Please input the column to generate: 4
1 2 3 4
8 7 6 5
9 10 11 12