package com.itheima; /* 判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串 */ import java.util.Scanner; public class Test1 {
public static boolean isSymmetry(String a){ //判断是否对称 for(int i=0;i<(a.length())/2;i++){ if(a.charAt(i)!=(a.charAt(a.length()-i-1))){ return false; } } return true; } public static void main(String[] args) { Scanner scanner =new Scanner(System.in); String a; System.out.println("请输入一个字符串:"); a=scanner.nextLine(); if(isSymmetry(a)){ System.out.println(a + ":" +"是对称字符串"); } else{ System.out.println(a + ":" +"不是对称字符串"); } } }
|