修改后: public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = cin.nextInt(); } HashMap<Integer, Integer> maps = new HashMap<>(); int xor = 0; int result = 0; for(int i=0;i<n;i++){ if (a[i]==0) { result ++; xor = 0; maps.clear(); }else{ xor = xor ^ a[i]; if (xor==0) { result ++; maps.clear(); }else if (maps.containsKey(xor)){ result ++; xor = 0; //此处增加 maps.clear(); }else{ maps.put(xor, 1); } } } System.out.println(result); }