实现文字的竖排

 1 public class Valigin {
2
3 /**
4 * @param args
5 * 实现文字竖排,看谁不回帖
6 * 修改。。
7 */
8 public static void main(String[] args) {
9 String str="锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。";
10 String txt = valign(str, 5);
11 System.out.println(txt);
12 }
13 public static String valign(String txt, int charPerCol) {
14 int size = txt.length();
15 int rows = charPerCol;
16 int cols = size / rows;
17 if (size % charPerCol != 0) {
18 cols++;
19 }
20 char[] ch = new char[rows * cols];
21 int i = 0;
22 for (int col = cols - 1; col >= 0; col--) {
23 for (int row = 0; row < rows; row++) {
24 if(i<size)
25 {
26 ch[row * cols + col] = txt.charAt(i++);
27 }else
28 ch[row * cols + col]='';//全角符号
29 }
30 }
31 String s ="";
32 for(int j=0;j<ch.length;j++)
33 {
34 s+=ch[j];
35 if((j+1)%cols==0)
36 s+="\n";
37 }
38 return s;
39 }
40 }

  

posted @ 2011-07-26 11:18  牧涛  阅读(371)  评论(0编辑  收藏  举报