How to add a new method called numberOfDigits() in java?

前端 未结 4 1029
面向向阳花
面向向阳花 2021-01-29 04:19

I have a program called public class ZeroCounter {. I want to add a new method to it called numberOfDigits() and add a line to test it in the main() method. How should I go abo

4条回答
  •  逝去的感伤
    2021-01-29 04:38

    Try this method to count digits of an integer number:-

     public int numberOfZeros(int number){
        return ((int) Math.log10(number) + 1);
    }
    

    Or you can try this code

          //Declare input as scanner
            Scanner input = new Scanner(System.in);
    
            //Take input
             System.out.println("Enter Number :");
             no = input.nextInt();
             System.out.println("Enter digit :");
             digit = input.nextInt();
    
            //add while loop
            while(no>0)
            {
                m=no%10;
                if(m==digit)
                    oc++;
                no=no/10;
            }
    
            System.out.println("Digit occurred "+oc+" times");
    

提交回复
热议问题