T3.java 856 B

123456789101112131415161718192021222324252627282930313233
  1. package com.loveCoding.homework.j20250517_method;
  2. import java.util.Scanner;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title T3
  7. * @description
  8. * **3. 元素查找器**
  9. * 要求:输入一个数字,判断它是否存在于数组中,并统计出现次数
  10. * 示例输入:数组`{2,5,3,2,8,2}`,查找数字2
  11. * 示例输出:存在,出现3次
  12. * @create 2025/5/24
  13. */
  14. public class T3 {
  15. public static void main(String[] args) {
  16. Scanner sc=new Scanner(System.in);
  17. int[] arr={2,5,3,2,8,2};
  18. int count=0;
  19. int n=sc.nextInt();
  20. for (int i = 0; i < arr.length; i++) {
  21. if (n==arr[i]){
  22. count++;
  23. }
  24. }
  25. if (count>0){
  26. System.out.println("存在,出现"+count+"次");
  27. }else {
  28. System.out.println("不存在");
  29. }
  30. }
  31. }