如何在java中添加一个名为numberOfDigits()的新方法? [英] How to add a new method called numberOfDigits() in java?

查看:179
本文介绍了如何在java中添加一个名为numberOfDigits()的新方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为public class ZeroCounter {的程序。我想为它添加一个名为numberOfDigits()的新方法,并在main()方法中添加一行来测试它。我应该怎么做呢?以下是代码的一小部分。

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 about doing it? Below is a small part of the code.

public class ZeroCounter   {
public static void main(String[] args)
    {
         System.out.println("Enter a nonnegative number:");
         Scanner keyboard = new Scanner(System.in);
         int number = keyboard.nextInt( ) ;

         System.out.println(number + " contains " + numberOfZeros(number) + " zeros.");
} // End of main

更新代码:

import java.util.*;

public class ZeroCounter   {
public static void main(String[] args)
    {
         System.out.println("Enter a nonnegative number:");
         Scanner keyboard = new Scanner(System.in);
         int number = keyboard.nextInt( ) ;

         System.out.println(number + " contains " + numberOfZeros(number) + " zeros.");
         int theDigit = keyboard.nextInt( ) ;    //added for the digitcount
         int digitCount = numberOfDigits(number, theDigit); 

    }// End of main

// * * * * Recursive Method * * * * 
public static int numberOfZeros(int n)  {
n = Math.abs(n); // Make sure the number is not negative. 

// 1. STOPPING CONDITION: Number has only only digit. 
if ( n < 10 )  {
    // if( n == 0 ) return 1;   else return 0;

    return n==0 ? 1:  0 ; // Conditional  operator.

} // end of the outer if block handling the stopping condition.


// 2. Else handle the case of two or more digits using recursion.
else {

    return n%10 == 0 ? 1 + numberOfZeros(n/10):  numberOfZeros(n/10) ; // Conditional  operator.


    // if (n%10 == 0) return 1 + numberOfZeros(n/10);
    // else return numberOfZeros(n/10);
} // end of outer else block 


} // end of recursive method method numberOfZeros. * * * * * * * * 


public static int numberOfDigits(int n, int digit) {
n = Math.abs(n); // Make sure the number is not negative.  
if ( n < 10 )  { 
     if( n == digit )    return 1;  
else           return 0; 
} // end of the outer if block handling the stopping condition. 
// 2. Else handle the case of two or more digits using recursion. 
else {   
      if (n%10 == digit) return 1 + numberOfDigits(n/10, digit);
        else return numberOfDigits(n/10, digit); 
} // end of outer else block 
} // end of recursive method method numberOfZeros. * * * * * * * *  
}


推荐答案

尝试使用此方法计算整数的数字: -

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");

这篇关于如何在java中添加一个名为numberOfDigits()的新方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆