问题描述:对于输入的指定字符串,例如“123456789”,实现相应的反转变为“987654321”。
问题分析:先将传入的字符串存储到字符数组里面
代码实现:
package com.yonyou.test;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
/**
* 测试类
* @author 小浩
* @创建日期 2015-4-18
*/
public class Test{
public static void main(String[] args) throws MalformedURLException, URISyntaxException{
new Test().reverse("123456789");
}
/**
* 987654321的字符串变成123456789
*/
public void reverse(String str){
char[] strChar=str.toCharArray();
char temp;
for(int i=0;i<str.length()/2;i++)
{
temp=strChar[i];
strChar[i]=strChar[str.length()-i-1];
strChar[str.length()-i-1]=temp;
}
String newStr=new String(strChar);
System.out.println("原始的字符串为:"+str);
System.out.println("当前的字符串为:"+newStr);
}
}