2016-03-05 54 views
0

大家好我不知道如何在同一行打印这两个语句。在同一行上打印两个不同的环

for(int index = 1; index < word.length(); index++){ 
     System.out.printf("\n%s", builder.charAt(index)); 
    } 

    for(int index = word.length()-2; index >= 0; index--){ 
     System.out.printf("\n%s%s",space ,backWards.charAt(index)); 
    } 

我的目标是给这个方法一个字和它打印矩形,例如:
字= java的
它将打印:
的java
AV
VA
avaj

请尽量保持尽可能简单,因为我仍然是初学者

回答

0

垂直打印字符串的唯一途径,是遍历它的人物和打印出来的每行一个

String str = "JAVA"; 
System.out.println(str); 
for(int i=1; i<str.length()-1; i++){ 
    System.out.print(str.charAt(i)); 
    for(int j=1; j<str.length()-1; j++){ 
     System.out.print(' '); 
    } 
    System.out.println(str.charAt(str.length()-1-i)); 
} 
for(int i=0; i<str.length(); i++){ 
    System.out.print(str.charAt(str.length()-1-i)); 
} 
0

下面是做到这一点的一种方法:

  String word = "java"; 
      // print first line 
      System.out.println(word); 
      String spaces = getSpacesFor(word.length() - 2); 
      // print out middle lines 
      for(int i = 1; i < word.length() - 1; i ++) { 
       // first character is from the normal word order 
       String s = "" + word.charAt(i); 
       // add middle spaces 
       s += spaces; 
       // add last character which is backwards order 
       s += word.charAt(word.length() - i - 1); 
       // print out 
       System.out.println(s); 
      } 

      // print backwards word 
      for(int i = 0; i < word.length(); i ++) { 
       System.out.print(word.charAt(word.length() - i - 1)); 
      } 

getSpacesFor将是一个方法如:

public static String getSpacesFor(int num) { 
    String s = ""; 
    for(int i = 0; i < num; i ++) { 
     s += " "; 
    } 

    return s; 
}