62,623
社区成员
发帖
与我相关
我的任务
分享 public static void main(String[] args){
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);
}
}int k = 0;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));
}
}int k = 0;。。。能不能解释一下呢?实在看不明白。。。
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;
}
}
}