Java程序设计找幸运数 求解

【问题描述】

一组数中包含数字 8 最多的那个数是幸运数。输入 n 和 n 个整数,找出这 n 个数中的幸运数。如果输入的数中有多个幸运数,只输出第一个幸运数,如果所有数都不包含数字 8,则输出 "No"。要求编写方法 int ndigit(int num, int digit) 用来计算某个整数 num 中包含的数字 digit 的个数,在主函数 main 中调用 ndigit 方法来计算用户输入数据中包含的数字 8 的个数。
【输入形式】

通过键盘输入 n 及 n 个整数。

【输出形式】

在控制台打印找到的第一个幸运数,如果没有找到幸运数,打印 "No"。

【样例输入】

5 45 853 4285 888 8088
【样例输出】

888
【样例说明】
输入 5 个整数:45 853 4285 888 8088,找到两个幸运数:888 和 8088,程序输出第一个幸运数 888。


import java.util.Scanner;

 class Main {
    
    static int ndigit(int n, int k){
        int count = 0;
        while(n > 0){
            int one = n % 10;
            if (one == k){
                count++;
            }
            n /= 10;
        }
        return count;
    }

        public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int t = kb.nextInt();
        int max = 0;
        int show = 0;
        while(t > 0){
            int num = kb.nextInt();
            if(ndigit(num, 8) > max){
                max = ndigit(num, 8);
                show = num;
            }
            t--;
        }  
        
        if(max == 0){
            System.out.println("NO");
        }else{
            System.out.println(show);
        }
    }
}

img