1) 回文是一种从前向后和从后向前拼写都相同的字符串。编写一个递归方法:testPalindrome,如果数组里存储的字符串是一个回文,则方法返回boolean值为true,否则返回值为false。2)有一个一维数组charArray,长度为14个字符,编写程序随即从字符 ‘0’到字符‘9’和字符’a’,’b’,’c’,’d’共有14个字符,编写程序随即从字符数组中获取4个字符,组成一个字符串,要求该字符串可以转化为一个整数,如1234满足条件,a234不满足条件。

解决方案 »

  1.   

    这是我写的第二题:
    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);
    }



    }


    }
      

  2.   


    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;
    }
    }

      

  3.   

    2楼代码里有个int k = 0;能不能解释一下呢?实在看不明白
      

  4.   

    第一题:
    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));
            }
    }
    第二题:不明白你的要求。
      

  5.   

    我自己做的第一题
    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();
    }
    }
      

  6.   

    int k = 0;
    本来打算计数用的,结果没有用上,忘删了,不好意思!
      

  7.   

    这是我写的第二题: 
    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); 
    } } 
    }
    有点头晕
    看不太懂楼上的,加个注释好吗
      

  8.   

    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);
    }



    }现在看看能看懂了吗?