第一种笨方法
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 5 /** 6 * 先接收每一行 7 * 每一行按照空格分成不同的字符串数组 8 * 倒叙输出数组 9 * @author j14131 10 * 11 */ 12 public class 字符串反转加密 { 13 public static void main(String[] args) { 14 Scanner scanner=new Scanner(System.in); 15 while(scanner.hasNext()) { 16 String string=scanner.nextLine(); 17 int blankNum=0; 18 List<Integer> list=new ArrayList(); 19 list.add(0); 20 for(int i=0;i<string.length();i++) { 21 char blank=string.charAt(i); 22 int test=blank-' '; 23 if(test==0) { 24 blankNum++; 25 list.add(i); 26 } 27 } 28 list.add(string.length()); 29 int length=++blankNum; 30 String[] strList=new String[length]; 31 for(int i=0;i<length;i++) { 32 //System.out.println(string.substring(list.get(i), list.get(i+1))); 33 if(i!=0) 34 strList[i]=string.substring(list.get(i)+1, list.get(i+1)); 35 else { 36 strList[i]=string.substring(list.get(i), list.get(i+1)); 37 } 38 } 39 for(int i=length-1;i>=0;i--) { 40 if(i==0) 41 System.out.println(strList[i]); 42 else { 43 System.out.print(strList[i]+" "); 44 } 45 46 } 47 48 } 49 } 50 51 }
第二种,直接使用方法切割
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 5 public class 字符串反转加密2 { 6 public static void main(String[] args) { 7 Scanner scanner=new Scanner(System.in); 8 while(scanner.hasNext()) { 9 String string=scanner.nextLine(); 10 String[] strList=string.split(" "); 11 for (int i = strList.length-1; i >=0; i--) { 12 String string1 = strList[i]; 13 if(i==0) 14 System.out.print(string1); 15 else 16 System.err.print(string1+" "); 17 } 18 } 19 scanner.close(); 20 } 21 }