Java中实现文字竖排 实现方法valign(String txt,int charPerCol),传入一个字符串,代表横排文字,传入一个整型值代表折行的位置。返回一个字符串,输出该字符串则横排文字已被转换为竖排文字(从左向右) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 说实话没看懂意思,不过个人觉得应该用println()这个换行方法实现吧 public class TestVAlign { public static void main(String[] args) { String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!"; int charPerCol = 4;// 每列的字数 new TestVAlign().valign(text, charPerCol); } private void valign(String text, int charPerCol) { if (charPerCol == 0) return; if (text == null || text.length() == 0) return; int colCount = getColCount(text, charPerCol);// 列数 int rowCount = getRowCount(text, charPerCol);// 行数 for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { if (col * charPerCol + row >= text.length()) break; System.out.print(text.charAt(col * charPerCol + row)); } System.out.println(); } } private int getRowCount(String text, int charPerCol) { if (text.length() < charPerCol) return text.length(); else return charPerCol; } private int getColCount(String text, int charPerCol) { int textLength = text.length(); if (textLength % charPerCol == 0) return textLength / charPerCol; else return textLength / charPerCol + 1; }}运行结果:楼要这吧对帮主的种,你助,就效希有!你是果望所 原来你是要先返回一个字符串,再输出这样的话代码修改如下,效果不变,还是楼要这吧对帮主的种,你助,就效希有!你是果望所public class TestVAlign { public static void main(String[] args) { String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!"; int charPerCol = 4;// 每列的字数 TestVAlign testVAlign=new TestVAlign(); String valignString = testVAlign.valign(text, charPerCol); System.out.print(valignString); } private String valign(String text, int charPerCol) { if (charPerCol == 0) return ""; if (text == null || text.length() == 0) return ""; int colCount = getColCount(text, charPerCol);// 列数 int rowCount = getRowCount(text, charPerCol);// 行数 String temp = ""; for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { if (col * charPerCol + row >= text.length()) break;//最后一列可能不满 temp += text.substring((col * charPerCol + row), (col * charPerCol + row) + 1); } temp+= "\n"; } return temp; } private int getRowCount(String text, int charPerCol) { if (text.length() < charPerCol) return text.length(); else return charPerCol; } private int getColCount(String text, int charPerCol) { int textLength = text.length(); if (textLength % charPerCol == 0) return textLength / charPerCol; else return textLength / charPerCol + 1; }} public class Valign { public static void main(String[] args) { String str = "实现文字竖排的方法"; String txt = valign(str, 5); System.out.println(txt); } public static String valign( String txt, int charPerCol) { int size = txt.length(); int rows = charPerCol; int cols = size/charPerCol; if(size%charPerCol!=0){ cols++; } char[] ch = new char[rows*cols]; int i=0; for(int col=cols-1; col>=0; col--){ for(int row=0; row<rows; row++){ if(i < size) ch[row*cols+col]=txt.charAt(i++); else ch[row*cols+col]='。'; } } String s = ""; for(int j=0; j<ch.length; j++){ s+=ch[j]; if((j+1)%cols==0) s+="\n"; } return s; }} public static void main(String[] args) { System.out.println(valign("楼主莫非要的是这种效果?", 4)); } public static String valign(String str, int size) { int temp = (str.length() - 1) / size + 1; char[] chars = new char[++temp * size]; char[] source = str.toCharArray(); for (int i = 0; i < chars.length; i++) { chars[i] = temp - i % temp < 2 ? '\n' : (temp - i % temp - 2) * size + i / temp >= source.length ? '~' : source[(temp - i % temp - 2) * size + i / temp]; } return new String(chars); } 竖排从右向左的话,代码稍作改动就可以了,但是下面这一行可能会有更好的处理方法:temp += " ";// 第一列可能不满,需要用空格补齐,这个宽度可能需要自己调整下运行结果如下:帮对吧这要楼助你,种的主!有希效就, 所望果是你public class TestVAlign { public static void main(String[] args) { String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!"; int charPerCol = 4;// 每列的字数 TestVAlign testVAlign = new TestVAlign(); String valignString = testVAlign.valign(text, charPerCol); System.out.print(valignString); } private String valign(String text, int charPerCol) { if (charPerCol == 0) return ""; if (text == null || text.length() == 0) return ""; int colCount = getColCount(text, charPerCol);// 列数 int rowCount = getRowCount(text, charPerCol);// 行数 String temp = ""; for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { if ((colCount - 1 - col) * charPerCol + row >= text.length()) temp += " ";// 第一列可能不满,需要用空格补齐,这个宽度可能需要自己调整下 else temp += text.substring((colCount - 1 - col) * charPerCol + row, (colCount - 1 - col) * charPerCol + row + 1); } temp += "\n"; } return temp; } private int getRowCount(String text, int charPerCol) { if (text.length() < charPerCol) return text.length(); else return charPerCol; } private int getColCount(String text, int charPerCol) { int textLength = text.length(); if (textLength % charPerCol == 0) return textLength / charPerCol; else return textLength / charPerCol + 1; }} 楼上的 你那结果输出有点不对啊 能不能调试下 给个正确的代码 我是java菜鸟 在学java 谢谢! 确认个基础问题,java中基本数据类型可以被序列化? 求助!关于Oracle9i中的基础问题 这个正则表达式的Pattern如何写? 有没有人用过JAVA处理TIF格式图象的JIMI包,SUN提供的. 第一个小程序就报错,不知何故?环境变量已经改过了,而且.class文件也生成了。请高手指点! JMF的问题 处学者的问题!有原代码,近来帮帮忙! 请教高手mtdropdown是作什么的,请讲地详细些 问一道算法题 如何使用mina接收大数据(数据长度为2000-10000) jdbc注册驱动问题 低级的java泛型问题 <T> T
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
new TestVAlign().valign(text, charPerCol); } private void valign(String text, int charPerCol) {
if (charPerCol == 0)
return;
if (text == null || text.length() == 0)
return;
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if (col * charPerCol + row >= text.length())
break;
System.out.print(text.charAt(col * charPerCol + row));
}
System.out.println();
} } private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
} private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1; }}运行结果:楼要这吧对帮
主的种,你助
,就效希有!
你是果望所
这样的话代码修改如下,效果不变,还是楼要这吧对帮
主的种,你助
,就效希有!
你是果望所
public class TestVAlign {
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
TestVAlign testVAlign=new TestVAlign();
String valignString = testVAlign.valign(text, charPerCol);
System.out.print(valignString);
} private String valign(String text, int charPerCol) {
if (charPerCol == 0)
return "";
if (text == null || text.length() == 0)
return "";
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
String temp = "";
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if (col * charPerCol + row >= text.length())
break;//最后一列可能不满
temp += text.substring((col * charPerCol + row), (col
* charPerCol + row) + 1);
}
temp+= "\n";
}
return temp;
} private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
} private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1; }}
public static void main(String[] args) {
String str = "实现文字竖排的方法";
String txt = valign(str, 5);
System.out.println(txt);
} public static String valign(
String txt, int charPerCol) {
int size = txt.length();
int rows = charPerCol;
int cols = size/charPerCol;
if(size%charPerCol!=0){
cols++;
}
char[] ch = new char[rows*cols];
int i=0;
for(int col=cols-1; col>=0; col--){
for(int row=0; row<rows; row++){
if(i < size)
ch[row*cols+col]=txt.charAt(i++);
else
ch[row*cols+col]='。';
}
}
String s = "";
for(int j=0; j<ch.length; j++){
s+=ch[j];
if((j+1)%cols==0)
s+="\n";
}
return s;
}
}
public static void main(String[] args) {
System.out.println(valign("楼主莫非要的是这种效果?", 4));
}
public static String valign(String str, int size)
{
int temp = (str.length() - 1) / size + 1;
char[] chars = new char[++temp * size];
char[] source = str.toCharArray();
for (int i = 0; i < chars.length; i++)
{
chars[i] = temp - i % temp < 2 ? '\n' :
(temp - i % temp - 2) * size + i / temp >= source.length ? '~' :
source[(temp - i % temp - 2) * size + i / temp];
}
return new String(chars);
}
temp += " ";// 第一列可能不满,需要用空格补齐,这个宽度可能需要自己调整下
运行结果如下:
帮对吧这要楼
助你,种的主
!有希效就,
所望果是你
public class TestVAlign {
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
TestVAlign testVAlign = new TestVAlign();
String valignString = testVAlign.valign(text, charPerCol);
System.out.print(valignString); } private String valign(String text, int charPerCol) {
if (charPerCol == 0)
return "";
if (text == null || text.length() == 0)
return "";
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
String temp = "";
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if ((colCount - 1 - col) * charPerCol + row >= text.length())
temp += " ";// 第一列可能不满,需要用空格补齐,这个宽度可能需要自己调整下
else
temp += text.substring((colCount - 1 - col) * charPerCol
+ row, (colCount - 1 - col) * charPerCol + row + 1);
}
temp += "\n";
}
return temp; } private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
} private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1; }}