1) 回文是一种从前向后和从后向前拼写都相同的字符串。编写一个递归方法:testPalindrome,如果数组里存储的字符串是一个回文,则方法返回boolean值为true,否则返回值为false。2)有一个一维数组charArray,长度为14个字符,编写程序随即从字符 ‘0’到字符‘9’和字符’a’,’b’,’c’,’d’共有14个字符,编写程序随即从字符数组中获取4个字符,组成一个字符串,要求该字符串可以转化为一个整数,如1234满足条件,a234不满足条件。
解决方案 »
- 下面这段代码中public String toString()方法在哪里调用啊?整个程序的运行结构看不太明白,请指点一下.
- 这两种方法,那种效率更高呢?
- 从数组里获得随机数的问题
- 测试
- POI操作excel,如何删除或隐藏一个sheet?
- 关于中文乱码的问题
- 如何得到当前JAVA程序运行时间?
- 如果进行垃圾回收!!!! 垃圾回收有何用处???? 请高手指教!!!!!!!!!!!!!!!!
- <<thinking in java 3rd edition>>
- 高手进来看看这个问题
- 【新手提问】请问有没有这样的事件监听器,当光标移到下个JTextField的时检查上面那个JTextField的内容?
- 整列数据的交换
public class TestChar{ public static void main(String[] args){
int index;
int num = 0;
String s = "";
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};
for( int i = 0; i < 4; i++){
int k = 0;
if(i == 0){
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{
index = (int)(Math.random() * 14);
s = s + charArray[index];
}
}
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1));
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
}
}
}
public static boolean testPalindrome(Object[] array){
if (array == null || array.length == 0){
return false;
}else if(array.length == 1){
return true;
}else if(array.length == 2){
if(array[0].equals(array[1])){
return true;
}else{
return false;
}
}else{
if (array[0].equals(array[array.length-1])){
Object[] _temp = new Object[array.length - 2];
System.arraycopy(array, 1, _temp, 0, _temp.length);
return testPalindrome(_temp);
}else{
return false;
}
}
}
public class Main
{
public static boolean testPalindrome(char[] s, int i, int j)
{
return i >= j? true: s[i] == s[j] && testPalindrome(s, i+1, j-1);
} public static void main(String[] argv)
{
String s = new String("12321");
System.out.println(testPalindrome(s.toCharArray(), 0, s.length() - 1));
}
}
第二题:不明白你的要求。
public class TestHui
{
//用递归判断回文
public boolean testPalindrome(String s)
{
if(s!="")//判断传入的字符是否为空
{
int i= 0,j=s.length()-1;
for(;i<j;i++)
{
for(;j>i;j--)
{
//比较首尾字符字符是否相同
if((s.charAt(i))!=(s.charAt(j)))
{
return false;
}
else
{
//截取字符串首尾后向前一位的子串
String s1=s.substring(i++,j);
testPalindrome(s1); //用testPalindrome(String s)方法递归 }
}
}
return true;
}
else
{
System.out.println("字符串为空!");
return false;
}
}
public static void main(String[] args)
{
TestHui t=new TestHui();
String s="qooggfgq";
if(t.testPalindrome(s))
{
System.out.println(s+":是回文");
}
else
System.out.println(s+":不是回文!");
System.out.println();
}
}
本来打算计数用的,结果没有用上,忘删了,不好意思!
public class TestChar{ public static void main(String[] args){
int index;
int num = 0;
String s = "";
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};
for( int i = 0; i < 4; i++){ int k = 0;
if(i == 0){
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{ index = (int)(Math.random() * 14);
s = s + charArray[index];
} }
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1));
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
} }
}
有点头晕
看不太懂楼上的,加个注释好吗
int index; //存储charArray数组下标
int num = 0; //存储转换后的数字
String s = ""; //存储随机生成的四位数
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};
for( int i = 0; i < 4; i++){
if(i == 0){ //第一位数不能是字母,否则无法转换成数字
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{
index = (int)(Math.random() * 14);
s = s + charArray[index];
}
}
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1)); //将得到的四个数的字符串一位一位转换成数字,遇到字母,结束转换。
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
}
}现在看看能看懂了吗?