本帖最后由 jiasuer008 于 2011-03-04 23:24:36 编辑

解决方案 »

  1.   

    package day05;import java.util.Arrays;
    import java.util.Scanner;public class ToVolign { /**
     * 实现文字的竖排
     */
    public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
         while(true){
    System.out.print("输入一行方字:");
    String str=sc.nextLine();
    if(str.equals("quit")){
    System.out.println("下次再见,bye!");
    break;
    }
    String s=valign(str,4);
    System.out.print(s);
    }

    } /**
     * 将输入文字竖排输出为字符串
     * 
     * @param str
     *            输入的文本
     * @param size
     *            每个竖排包含的文字数量
     * @return 竖排文本,
     */
    public static String valign(String str, int size) {
    int rows = size;
    // int cols=str.length()%size==0 ?
    // str.length()/size:str.length()/size+1;
    int cols = str.length() / size;  //cols是列
    //System.out.println(size);
    if (str.length() % size != 0) {
    cols++;  
    }
    System.out.println("列数是"+cols);
    char[] src = new char[cols * rows]; //rows 是行数
    Arrays.fill(src, '~');// 将src数组变成(~~~~~~~~~~~~~}12个
    System.arraycopy(str.toCharArray(), 0, src, 0, str.length()); // 复制数组变成{开始测试我们的软件系统~}
    // 如果输入"开始测试我们的软件系统"   将str数组的零位开始赋给src数组的零位  数组长度
    int i = 0;
    char[] chs = new char[cols * rows];
    for (int col = cols - 1; col >= 0; col--) {
    for (int row = 0; row <= rows - 1; row++) {
    int idx = row * cols + col;// 目标数组位置序号
    //System.out.println(row+"+"+cols+"+"+col+"+"+idx );
    chs[idx] = src[i++];// str.charAt(i++)
    }
    }
    String s = "";
    for (int j = 0; j < chs.length; j++) {
    char c = chs[j];
    System.out.println(j);
    s += c;
    //System.out.println(s);
    if ((j + 1) % cols == 0) {
    s += "\n";
    }
    }
    return s;
    }
    }我的没用2维数组
      

  2.   

    package day04;import java.util.Scanner;
    /**
     * 打印一首诗
     * 
     *
     */
    public class Valign {
    public static void main(String[] args) {
    Scanner console=new Scanner(System.in);
    String str=console.nextLine();
    char[][]  arr=new char[5][4];
    int count=0;

    System.out.println("原序:");
    for (int i =  arr[0].length-1; i >=0; i--) {
    for (int j = 0; j < arr.length; j++) {
    arr[j][i]=str.charAt(count++);
    System.out.print(arr[j][i]);
    }
    System.out.println();
    }

    System.out.println("现序:");
    for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[0].length; j++) {
    System.out.print(arr[i][j]);

    }
    System.out.println();
    }
    }
    }这个也是刚写的,只能写规定字符数的