计数整数的出现在一个数组 [英] Counting occurrences of integers in an array

查看:145
本文介绍了计数整数的出现在一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写整数计算的出现进入到一个数组,例如,如果你输入1 1 1 1 2 1 3 5 2 3的程序,该程序将打印出不同数字其次是它们的出现,像这样

I'm writing a program that counts the occurrences of integers entered into an array, eg if you entered 1 1 1 1 2 1 3 5 2 3, the program would print out the distinct numbers followed by their occurrences, like this:

1时的5倍,
2发生的2倍,
3出现2次,
5出现1次

1 occurs 5 times, 2 occurs 2 times, 3 occurs 2 times, 5 occurs 1 time

和它的基本完成,除一个问题我想不通:

And it's almost finished, aside from one problem I can't figure out:

import java.util.Scanner;
import java.util.Arrays;
public class CountOccurrences
{
   public static void main (String [] args)
   { 

    Scanner scan = new Scanner (System.in);

    final int MAX_NUM = 10;  

    final int MAX_VALUE = 100;

    int [] numList;

    int num;

    int numCount;

    int [] occurrences; 

    int count[];

    String end;

    numList = new int [MAX_NUM];

    occurrences = new int [MAX_NUM];

    count = new int [MAX_NUM];

 do
  {
     System.out.print ("Enter 10 integers between 1 and 100: ");

     for (num = 0; num < MAX_NUM; num++)
     {
        numList[num] = scan.nextInt();
     }

     Arrays.sort(numList);

     count = occurrences (numList); 

     System.out.println();   

     for (num = 0; num < MAX_NUM; num++)
     {
        if (num == 0)
        {
           if (count[num] <= 1)
              System.out.println (numList[num] + " occurs " + count[num] + " time");

           if (count[num] > 1)
              System.out.println (numList[num] + " occurs " + count[num] + " times");
        } 

        if (num > 0 && numList[num] != numList[num - 1])
        {
           if (count[num] <= 1)
              System.out.println (numList[num] + " occurs " + count[num] + " time");

           if (count[num] > 1)
              System.out.println (numList[num] + " occurs " + count[num] + " times");
        }   
     }          

     System.out.print ("\nContinue? <y/n> ");
     end = scan.next(); 

  } while (!end.equalsIgnoreCase("n"));
}


 public static int [] occurrences (int [] list)
 {
      final int MAX_VALUE = 100;

      int num;

      int [] countNum = new int [MAX_VALUE];

      int [] counts = new int [MAX_VALUE];

      for (num = 0; num < list.length; num++)
  {
     counts[num] = countNum[list[num]] += 1;
  }

  return counts;
 } 
}

我遇到的问题是,无论什么NUM当前值,计数只打印出1,这个问题是不是在计数的发生,因为当你输入数字的方法在可变的位置,值的变化

The problem I'm having is that no matter what the current value for 'num' is, 'count' only prints out 1, and the problem isn't in the method that counts the occurrences, because when you enter numbers in the place of the variable, the value changes.

有没有办法,我可以改变这一点,以便它会正确地打印出发生的任何方式,或者我应该试试别的?
而简单的解决方案,就更好了,因为我还没有走过去一维数组。

Is there any way that I can alter this so that it'll print out the occurrences properly, or should I try something else? And the simpler the solution, the better, as I haven't yet gone past single dimension arrays.

感谢您的帮助!

推荐答案

我有什么要说的是,我花了一段时间来找出这两个变量计数 countNum 重新present了,也许还需要一些意见。但最后我发现的bug。

What I have to say is, it took me a while to figure out what the two variables count and countNum represent for, maybe some comments are needed. But finally I find out the bug.

假设输入十个号码是: 5,6,7,8,5,6,7,8,5,6

Suppose input ten numbers are: 5, 6, 7, 8, 5, 6, 7, 8, 5, 6

排序后, numList 是: 5,5,5,6,6,6,7,7,8,8

阵列计数出现()应返回: [1,2 ,3,1,2,3,1,2,1,2]

其实在这个结果数组中的唯一有用的数字是:

Actually the only useful numbers in this result array are:

count[2]: 3     count number for numList[2]: 5
count[5]: 3     count number for numList[5]: 6
count[7]: 2     count number for numList[7]: 7
count[9]: 2     count number for numList[9]: 8

其他的数字,像前两个数字 1,2 3 ,只用来计算总之逐步出现,对不对?所以,你的循环逻辑应更改如下:

The other numbers, like the first two numbers 1, 2 before 3, are only used to calculate the sum occurrences incrementally, right? So, your loop logic should be changed as following:


  1. 删除第一个如果 code座:

if (num == 0)
{
   if (count[num] <= 1)
      System.out.println (numList[num] + " occurs " + count[num] + " time");

   if (count[num] > 1)
      System.out.println (numList[num] + " occurs " + count[num] + " times");
} 


  • 将第二个如果条件:

    if ((num + 1) == MAX_NUM || numList[num] != numList[num + 1]) {
        ......
    }
    


  • 在此之后,按照预期的code应该很好地工作。

    After this, your code should work well as expected.

    顺便说一句,你真的不需要做这样错综复杂。刚刚尝试的HashMap :)

    BTW, you really don't need to do it so intricately. Just try HashMap :)

    这篇关于计数整数的出现在一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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