实现方法valign(String txt,int charPerCol),传入一个字符串,代表横排文字,传入一个整型值代表折行的位置。返回一个字符串,输出该字符串则横排文字已被转换为竖排文字(从左向右)

解决方案 »

  1.   

    说实话没看懂意思,不过个人觉得应该用println()这个换行方法实现吧
      

  2.   

    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; }}运行结果:楼要这吧对帮
    主的种,你助
    ,就效希有!
    你是果望所
      

  3.   

    原来你是要先返回一个字符串,再输出
    这样的话代码修改如下,效果不变,还是楼要这吧对帮
    主的种,你助
    ,就效希有!
    你是果望所
    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; }}
      

  4.   

    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;
    }
    }
      

  5.   


    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);
        }
      

  6.   

    竖排从右向左的话,代码稍作改动就可以了,但是下面这一行可能会有更好的处理方法:
    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; }}
      

  7.   

    楼上的 你那结果输出有点不对啊 能不能调试下 给个正确的代码 我是java菜鸟 在学java 谢谢!