打印特定数字在数字中出现的次数

huangapple go评论59阅读模式
英文:

Print how many times appears a specific digit in a number

问题

以下是翻译好的代码部分:

public class loopPrr {

    public void howMany0(int i) {
        int howmany = 0;
        char z = '0';
        while (i > 0) {
            if (i % 10 == z) {
                howmany++;
            }
            i /= 10;
        }
        System.out.println(howmany);
    }

    public static void main(String args[]) {
        loopPrr n = new loopPrr();
        n.howMany0(32001);
    }
}
英文:

The return type is void and there is one input parameter and I am trying to use a while loop to print out how many of a certain number there is an the input integer.

So as an example int i = 32001; then I want too know how many 0's there are in the int and the output would be 2 cause there's only two 0's in that int.

My code below for some reason only outputs 0 and I don't know why. Could someone pls fix or help.

And please don't use any power tools or certain methods that do the work for you and you can use for loop or while loop.

public class loopPrr
{

public void howMany0(int i){
        
        
        int n = 0;
        int howmany = 0;
        char z = '0';
         while(i <= 0){
             ++i;
            if(i == z){
                howmany++;  
            }
        }
       
         System.out.println(howmany);
    }


public static void main(String args[]){
        
        loopPrr n = new loopPrr();
    
        
        n.howMany0(32001);
        
        
    
    
    }





}



答案1

得分: 1

尝试下面的代码。逻辑非常简单,执行模10运算以获取最右边的数字,如果是目标数字则进行计数,除以10以去除最后一位数字,然后重复执行。

public void countNumber(int n, final int target) {

    int count = 0;

    if (n == target) {
        count = 1;
    }
    else {
        while (n != 0) {
            if (n % 10 == target)
                count++;

            n /= 10;
        }
    }

    System.out.println(count);
}
英文:

Try the next code. The logic is very simple, perform a mod 10 to get the most-right digit, count it if is the target, divide by 10 to remove the last digit, and repeat.

public void countNumber(int n, final int target) {

    int count = 0;

    if (n == target) {
        count = 1;
    }
    else {
        while (n != 0) {
            if (n % 10 == target)
                count++;

            n /= 10;
        }
    }

    System.out.println(count);
}

答案2

得分: 0

或者,您可以将您的数字拆分为字符数组,然后查看其中有多少个零:

public int 计算零的个数(int i) {
    if (i == 0) return 1;
    String str = String.valueOf(i);
    char[] digits = str.toCharArray();
    int 零的个数 = 0;
    char= '0';
    for (char c : digits) {
        if (c ==) {
            零的个数++;
        }
    }
    return 零的个数;
}
英文:

Alternatively, you can split your number into a character array and see how many zeros you have:

public void howMany0(int i) {
    if (i == 0) return 1;
    String str = String.valueOf(i);
    char[] digits = str.toCharArray();
    int howmany = 0;
    char z = '0';
    for (char c : digits) {
        if (c == z) {
            howmany++;
        }
    }
    return howmany;
}

答案3

得分: 0

一个简单的获取所有数字频率计数的方法如下所示:

int[] freq = new int[10];
int numb = 2829921;
while (numb > 0) {
    // 使用数字作为索引并增加计数
    freq[numb % 10]++;
    numb /= 10;
}
for (int i = 0; i < 10; i++) {
    // 跳过空槽
    if (freq[i] > 0) {
        int k = freq[i];
        System.out
            .println(k + " " + i + ((k > 1) ? "'"s" : ""));
    }
}

打印结果为

1 1
3 2's
1 8
2 9's
英文:

A simple way to get a frequency count of all the digits is as follows:

int[] freq = new int[10];
int numb = 2829921;
while (numb &gt; 0) {
    // use the digit as the index and bump the count
	freq[numb % 10]++;
	numb /= 10;
}
for (int i = 0; i &lt; 10; i++) {
    // skip empty slots
	if (freq[i] &gt; 0) {
		int k = freq[i];
		System.out
				.println(k + &quot; &quot; + i + ((k &gt; 1) ? &quot;&#39;s&quot; : &quot;&quot;));
	}
}

Prints

1 1
3 2&#39;s
1 8
2 9&#39;s


</details>



huangapple
  • 本文由 发表于 2020年9月30日 04:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64127354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定