实现文字的竖排

本文介绍了一个使用Java编写的简单程序,该程序可以将输入的文字转换为竖排格式进行打印。通过对字符串的操作和字符的重新排列,实现了独特的文字展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值