将一个字符串str的内容颠倒过来,并输出。
数据范围:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] chars = str.toCharArray(); int left = 0; int right = chars.length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } System.out.println(new String(chars)); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { // 方法一 StringBuilder str = new StringBuilder(in.nextLine()); System.out.print(str.reverse()); // 方法二 // for (int i = input.length() - 1; i >= 0; i--) { // System.out.print(input.charAt(i)); // } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { StringBuilder sb = new StringBuilder(sc.nextLine()).reverse(); System.out.print(sb.toString()); } } }哈哈哈,能多偷懒就多偷懒
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { // 注意 while 处理多个 case StringBuilder b = new StringBuilder(in.nextLine()); System.out.println(b.reverse()); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String a = in.nextLine(); StringBuilder su = new StringBuilder(a); System.out.println(su.reverse().toString()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while ((str = br.readLine()) != null) { StringBuilder sb = new StringBuilder(str); sb = sb.reverse(); System.out.println(sb.toString()); } } }