java判断回文字符串几种简单的实现
1个回答
展开全部
import java.util.Scanner; public class Tststriu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入字符串的个数:"); int n = sc.nextInt(); String[] a = new String[n]; for(int i = 0;i < n;++i){ System.out.println("输入第"+(i+1)+"个字符串:"); a[i] = sc.next(); } int sum = 0; String max = null; for(int i = 0;i < n;++i){ if(isString(a[i])) sum++; if(bijao(a[i],max)){ max = a[i]; } } System.out.println("回文字符串个数是:"+sum); System.out.println("最大字符串是:"+max); } public static boolean isString(String s){ int n = s.length(); int i=0; for(i=0; i < n/2; i++) { if(s.charAt(i)!= s.charAt(n-1-i)) break; } if(i>=n/2) return true; else return false; } public static boolean bijao(String s1,String s2){ if(s2 == null) return true; int n = Math.min(s1.length(),s2.length()); for(int i = 0; i< n;++i){ if((int)s1.charAt(i)>(int)s2.charAt(i)) return true; else return false; } if(n==s1.length()) return false; else return true; } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询