1 package shb.java.demo; 2 3 public class MyTest { 4 public static void main(String[] args) { 5 String string = "For God so loved the world kdkdk"; 6 reverse_2(string); 7 } 8 //方法一 9 public static void reverse(String str){ 10 11 String[] strings = str.split(" "); 12 for(int i= 0 ; i < strings.length;i++){ 13 String top =strings[0]; 14 15 for(int j=1;j<strings.length-i;j++){ 16 17 strings[j-1]= strings[j]; 18 19 } 20 strings[strings.length-i-1]=top; 21 22 } 23 for(String string:strings){ 24 System.out.println(string); 25 } 26 } 27 //方法二 28 public static void reverse_2(String str_2){ 29 String []strings = str_2.split(" "); 30 String temp = ""; 31 if(strings.length%2==0){ 32 for(int i=0;i<strings.length/2;i++){ 33 temp = strings[i]; 34 strings[i] = strings[strings.length-1-i]; 35 strings[strings.length-1-i] = temp; 36 } 37 for(String string2:strings){ 38 System.out.println(string2); 39 } 40 }else { 41 for(int i=0;i<(strings.length-1)/2;i++){ 42 temp = strings[i]; 43 strings[i] = strings[strings.length-1-i]; 44 strings[strings.length-1-i] = temp; 45 } 46 for(String string2:strings){ 47 System.out.println(string2); 48 } 49 } 50 } 51 }