Recursion palindrome test summary judgment

 

Design ideas: First, to get the topic is a recursive method to determine whether a given string is a palindrome. The method first thought is simple for loop and if the judge, and then use the recursive method can be achieved.

The following source code, and operation results are given screenshot

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        String str;
        Scanner input=new Scanner(System.in);
        System.out.print("请输入:");
        str=input.next();
        boolean flag = find(str,0,str.length());
        if(flag==true) {
            System.out.print(str+"是回文");
        }
        else {
            System.out.print(str+"不是回文");
        }
    }
    private static boolean find(String str, int start, int length) {
        if(length<=1)
            return true;
        else if(str.toCharArray()[start]==str.toCharArray()[length-1]){
            return find(str,start+1,length-1);
        }
        return false;
    }
}

 

Guess you like

Origin www.cnblogs.com/shumouren/p/11586896.html