使用java代码片段返回对控制台输入字符串的镜像字符串
时间: 2023-08-09 21:02:16 浏览: 119
可以使用Java的字符串反转方法来返回控制台输入字符串的镜像字符串。下面是一个示例代码片段:
```java
import java.util.Scanner;
public class MirrorString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符串: ");
String input = scanner.nextLine();
String mirror = getMirrorString(input);
System.out.println("镜像字符串: " + mirror);
}
public static String getMirrorString(String input) {
StringBuilder builder = new StringBuilder(input);
return builder.reverse().toString();
}
}
```
在代码中,首先使用`Scanner`类来获取用户在控制台输入的字符串。然后,调用`getMirrorString`方法来获取镜像字符串。在`getMirrorString`方法中,我们使用了`StringBuilder`类来反转输入字符串,最后将反转后的字符串转换为正常字符串返回。最后,在`main`方法中将镜像字符串输出到控制台。
阅读全文
相关推荐


















