Package CN. Stringbuffer;import Java.util.Scanner;/** Requirements: Keyboard input string, using StringBuffer's reverse () method to invert the display **/ Public classFanzhuan { Public Static voidMain (string[] args) {Scanner sc=NewScanner (System.inch); System. out. println ("Please input string"); String Str=Sc.nextline (); //String result = Fanzhuan (str); //String result = fanzhuan2 (str);String result =fanzhuan3 (str); System. out. println ("The result after reversal is:"+result); } //string-"character array, get every character, then stitch Public Staticstring Fanzhuan (String s) {string result=""; Char[] ch =S.tochararray (); for(inti = ch.length-1; I >=0; i--) {result+=Ch[i]; } returnresult; } //Use the string charat to get each character, using length () to get the lengths Public Staticstring fanzhuan2 (String s) {string result=""; for(inti = s.length ()-1; I >=0; i--) {result+=S.charat (i); } returnresult; } //direct inversion using StringBuffer's reverse () method Public Staticstring Fanzhuan3 (string s) {/*StringBuffer buffer = new StringBuffer (s); StringBuffer result = Buffer.reverse (); return result.tostring ();*/ return NewStringBuffer (s). Reverse (). toString (); }}
stringbuffer-keyboard input string, using StringBuffer's reverse () method to invert the display