Java程序设计-统计数字填空题

【问题描述】

程序填空。输入一个整数,输出数字 7 在整数中出现的次数。例如,当输入为 -157 时,输出为 1;当输入为 77537 时,输出为3。

【输入形式】

通过键盘输入一个整数。

【输出形式】

程序统计数字 7 在整数中出现的次数,在控制台打印出来。

【样例输入】

-157
【样例输出】

1
【样例说明】

输入的整数为 -157,数字 7 在整数中出现的次数为 1 次。

img


Scanner sc = new Scanner(System.in);
        int k,count = 0;
        int a = sc.nextInt();
        System.out.println("sdfs" + a);
        if (a<0){
            a = -a;
        }
        while (a>0){
            k=a%10;
            a=a/10;

            if (k==7) {
                count++;
            }
        }
        System.out.println(count);
        sc.close();