`

回文数判断

 
阅读更多

 

import javax.swing.JOptionPane;
public class Palindrome
{  
    public static boolean method1(String inputValue){
        boolean flag = false;
        long longValue = Long.parseLong(inputValue);
        long temp = longValue;
        long reverseLong = 0L;
        while(longValue != 0)
        {
         reverseLong = reverseLong*10+longValue%10;
         longValue = longValue/10;
        }
        flag = reverseLong == temp? true:false;
        return flag;
    }    
    
    public static boolean method2(String inputValue){
        boolean flag = false;
        String org = inputValue;
        StringBuffer sb = new StringBuffer(inputValue);
        StringBuffer buffer = sb.reverse();
        String reverse = buffer.toString();
        flag = org.equals(reverse)? true:false;
        return flag;
    }
    
    public static boolean method3(String inputValue){
         boolean flag = true;
         int length= inputValue.length();
         char[] array = inputValue.toCharArray();
         for(int i =0;i<=length/2;i++){
             if(array[i] != array[length-i-1]){
                  flag = false;
                  break;
             }
        }
         return flag;
    }
    
     public static void main(String [] args)
     {
         String inputValue = JOptionPane.showInputDialog("please input value");
         System.out.println(method1(inputValue));
         System.out.println(method2(inputValue));
         System.out.println(method3(inputValue));
     }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics