使用递归方式判断某个字串是否是回文/递归编程解决汉诺塔问题。用Java实现

课后作业4:

  请看以下代码,你发现了有什么特殊之处吗?


public class MethodOverload {


public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println(" The square of double 7.5 is " + square(7.5));
}


public static int square(int x) {
return x * x;
}


public static double square(double y) {
return y * y;
}
}

 

       上述示例代码展示了Java的“方法重载(overload)”特性。

 满足以下条件的两个或多个方法构成“重载”关系:

      (1)方法名相同;

      (2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

注意:方法的返回值不作为方法重载的判断条件。

课后作业3:

    使用递归方式判断某个字串是否是回文

import java.util.Scanner;
public class HuiWeng {
	public static boolean isPalindrome(String s,int i,int j){  
        if(i > j)  
            throw new IllegalArgumentException();  
        if(i == j)  
            return true;  
        else{  
            return (s.charAt(i) == s.charAt(j)) && isPalindrome(s,i+1,j-1);  
        }  
    }  
      
    public static void main(String[] args){  
    	Scanner in=new Scanner(System.in);
    	String s = in.nextLine();
        int i = 0; 
        int j = s.length() - 1;  
        System.out.println(s + " is Palindrome? " + HuiWeng .isPalindrome(s, i, j));  
    }   
}

  

原文地址:https://www.cnblogs.com/xc166/p/5966341.html