java:查找数组中整数的频率

发布于 2024-12-11 03:12:44 字数 2339 浏览 0 评论 0原文

我需要开发一个 java 程序,要求用户输入一些整数并找到最大和最小的数字以及这些数字的平均值。然后,将数组集合划分为用户指定的多个子区间,然后生成一个边界点,每个边界点的长度为子区间宽度。

问题是我需要创建一个频率:

例如:间隔:

14.5-16.5

频率: 1(这里应该显示有多少个整数属于这个边界)

间隔:

16.5-18.5

频率:4等等。

这是我到目前为止的代码,除了找到每个边界的频率之外,几乎已经完成了。

import java.util.Scanner;
public class Sta
{
    public static void main(final String args[])
    {
        final Scanner input = new Scanner(System.in);

        int num=0;
        int range=0;
        int subnum;
        int subwid;

        System.out.print("How many numbers do you want to enter: ");
        num=input.nextInt();

        final int array[]=new int[num];

        System.out.print("Enter the numbers now: ");
        for(int i=0; i<array.length; i++)
        {
            array[i]=input.nextInt();
        }

        System.out.print("These are the numbers you entered:\n");
        printArray(array);

        int smallest=array[0];
        int largest=array[0];

        for (final int element : array) {
            if(element>largest) {
                largest=element;
            } else if(element<smallest) {
                smallest=element;
            }
            range=largest-smallest;
        }
        System.out.printf("Largest is %d\n",largest);
        System.out.printf("Smallest is %d\n",smallest);
        System.out.printf("Range is %d\n",range);

        System.out.print("Enter the number of subinterval: ");
        subnum=input.nextInt();

        subwid=range/subnum;
        System.out.printf("The width of subinterval is %d\n", subwid);

        /* this part should find the boundaries and find the elements that fall between
           the each two boundaries */
        for(double boundary=smallest-.5; boundary <=largest+.5; boundary +=subwid)
        {
        System.out.printf("Boundaries are %.1f\n",boundary);

        for(int element=0; element<array.length; element++)
          {
            if(element>=boundary)
              {
            System.out.printf("f=%d\n",element);
              }
          }
         }
    }

    public static void printArray(final int arr[])
    {

        for (final int element : arr) {
            System.out.print(element + "\n");
        }
    }

}

问题是如何找到上表示例中的频率?

I need to develop a java program that asks the user to enter some integers and find the largest, and smallest number, and the average of those numbers. Then, divides the set of array into a number of sub-intervals that the user specifies, then it generates a boundary points each has a length of sub-interval width..

The problem is that I need to create a frequency:

ex: Interval:

14.5-16.5

Frequency:
1 (here it should shows how many integers belong to this boundaries)

Interval:

16.5-18.5

Frequency: 4 and so on.

Here is the code I have so far, almost done except finding the frequency of each boundary..

import java.util.Scanner;
public class Sta
{
    public static void main(final String args[])
    {
        final Scanner input = new Scanner(System.in);

        int num=0;
        int range=0;
        int subnum;
        int subwid;

        System.out.print("How many numbers do you want to enter: ");
        num=input.nextInt();

        final int array[]=new int[num];

        System.out.print("Enter the numbers now: ");
        for(int i=0; i<array.length; i++)
        {
            array[i]=input.nextInt();
        }

        System.out.print("These are the numbers you entered:\n");
        printArray(array);

        int smallest=array[0];
        int largest=array[0];

        for (final int element : array) {
            if(element>largest) {
                largest=element;
            } else if(element<smallest) {
                smallest=element;
            }
            range=largest-smallest;
        }
        System.out.printf("Largest is %d\n",largest);
        System.out.printf("Smallest is %d\n",smallest);
        System.out.printf("Range is %d\n",range);

        System.out.print("Enter the number of subinterval: ");
        subnum=input.nextInt();

        subwid=range/subnum;
        System.out.printf("The width of subinterval is %d\n", subwid);

        /* this part should find the boundaries and find the elements that fall between
           the each two boundaries */
        for(double boundary=smallest-.5; boundary <=largest+.5; boundary +=subwid)
        {
        System.out.printf("Boundaries are %.1f\n",boundary);

        for(int element=0; element<array.length; element++)
          {
            if(element>=boundary)
              {
            System.out.printf("f=%d\n",element);
              }
          }
         }
    }

    public static void printArray(final int arr[])
    {

        for (final int element : arr) {
            System.out.print(element + "\n");
        }
    }

}

The question is how do I find the frequencies as in the above table example ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-12-18 03:12:44

以下是一些选项:

  • 嵌套循环:对于每个范围,计算属于该范围的数字数量。
  • '新整数[子编号]'。一次性计算每个范围内的项目数量。考虑使用除法和截断。
  • 对数字进行排序。计算每个边界之前出现的次数。

Here are some options:

  • Nested Loops: For each range, count the number of numbers that fall in that range.
  • 'new int[subnum]'. In one pass, count the number of items in each range. Consider using division and truncation.
  • Sort the numbers. Count how many occur before each boundary.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文