
正文
反转字符串java代码 java字符反转方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java字符串反转
数组下标 越界 这个是用数组常有错误!
代码:
import java.util.*;
public class Practise {
public static void main(String[] args) {
System.out.println("输入字符串: ");
Scanner S = new Scanner(System.in);
String b = S.nextLine();
char[] ch = b.toCharArray();
for (int i = ch.length-1;i =0; i--) {
System.out.print(ch[i]);
}
S.close();
}
}
相关问答
Q1: Java编程中如何反转字符串?
几种方法
一种是字符串转换为字符数组char[]反转字符串java代码,然后char[]重新排序后出新字符串
一种是通过StringBuffer或StringBuilder内置反转字符串java代码的reverse()方法排序
Q2: java反转字符串
mport java.util.*;
public class NameChange{
public static void main(String[] args){
Scanner console=new Scanner(System.in);
System.out.print("Please enter your full name:");
String s=console.nextLine();
System.out.print("Your name in reverse order is:");
//获取空格第一个空格符的位置
int a=s.indexOf(" ");
//打印输入字符串从空格开始到结束的子串
System.out.print(s.substring(a+1,s.length()));
System.out.print(" ");
//打印输入字符串从开始到空格的子串
System.out.print(s.substring(0,a));
}
}
输入zhongzhou han 输出han zhongzhou
Q3: Java中如何实现字符串反转?
反转:
public class test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入:");
String s1 = input.nextLine();//得到输入的字符e5a48de588b6e799bee5baa6e997aee7ad9431333363396464串
System.out.print("翻转后:");
for(int i=s1.length()-1;i=0;i--){
System.out.print(s1.charAt(i));
}
}
}
或者
import java.util.*;
public class StringChange{
public static void main(String[] args){
System.out.println("Please enter the String:");
String str = new Scanner(System.in).nextLine(); //输入字符串
String s2[] = str.split("\\s"); // \s 以空格为分隔符拆分字符串,并保存到数组s2里面
for (int i = s2.length-1; i = 0; i--) { //反向输出数组
System.out.print(s2[i]+" ");
}
}
}
Q4: 请教JAVA编程题“编程实现反转一个字符串并输出”
StringBuffer的常用方法 这个方法返回字符串缓冲区的当前容量。 public synchronized StringBuffer reverse() 这个方法反转字符串缓冲区中的字符串 public int length() 这个方法返回缓冲区中字符的个数 public synchronized setLength(int newLength) 这个方法设置字符串缓冲区的长度。 public synchronized charAt(int index) 这个方法返回字符串缓冲区中指定位置的字符 这个方法将字符串缓冲区中指定位置的字符设置为ch。
关于反转字符串java代码和java字符反转方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






