package com.loveCoding.homework.j20250517; /** * @author WanJl * @version 1.0 * @title T8 * @description * **8. 数组对称判断** * 要求:判断数组是否首尾对称 * 示例输入1:`{1,3,5,3,1}` → 输出:对称 * 示例输入2:`{2,4,4,2}` → 输出:对称 * 示例输入3:`{1,2,3,4}` → 输出:不对称 * @create 2025/5/24 */ public class T8 { public static void main(String[] args) { int[] arr={1,2,3,4}; boolean b=false; for (int i = 0; i < arr.length/2; i++) { if (arr[i]==arr[arr.length-1-i]){ //判断收尾元素是否相等 b=true; //相等则为b赋值为true }else {//否则赋值false b=false; break; //只要有一次赋值是false,就说明已经不对称了,后面的就不需要继续循环了。直接跳出循环。 } } if (b){ System.out.println("对称"); }else { System.out.println("不对称"); } } }