package com.wf.bean;
import java.util.Scanner;
import javax.sound.sampled.Line;
public class D {
/**
* A:案例演示 需求:把字符串反转 举例:键盘录入"abc" 输出结果:"cba" 分析: 1,通过键盘录入获取字符串Scanner
* 2,将字符串转换成字符数组 3,倒着遍历字符数组,并再次拼接成字符串 4,打印
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请从键盘输入字符串:");
String line = sc.nextLine();
char[] arr = line.toCharArray(); //将字符串转换为字符数组
String s = "";
for (int i = arr.length-1; i >= 0; i--) {
s = s + arr[i];
}
System.out.println(s);
}
}
|
|