找出唯一数字的频率

时间:2013-10-25 07:47:54

标签: java

我正在努力解决Java中的问题,这是我的任务的一部分。问题如下:

用户在屏幕提示时逐个输入十个数字。然后屏幕将所有不同的值分配给一个数组和一个类似的数组,以保持这些数字出现次数的频率。

我已完成以下工作,但似乎我被困在某个地方为数组分配频率和不同的值:

import java.util.*;

public class JavaApplication10 
{
    public static void main(String[] args)
    {
       int [] numbers = new int [10];
       int [] count = new int[10];
       int [] distinct = new int[10];

       for (int k=0;k<10;k++)
       {
           count[k]=0;
           distinct[k]=0;
       }
       java.util.Scanner input = new java.util.Scanner(System.in);

       System.out.print("Enter number 0: ");
       numbers[0]=input.nextInt();
       count[0]=1;
       distinct[0]=numbers[0];
       int j=0;
       for (int i = 1;i<10;i++)
       {
           System.out.print("Enter number "+i+": ");
           numbers[i]=input.nextInt();

           while(j<i)
           {
               if (distinct[j]==numbers[i])
               count[j]=count[j]+1;
               else
                   distinct[j+1]=numbers[i];
               j++;
           }
       }
    for (int k=0;k<10;k++)
    {
        System.out.println(distinct[k]+ " "+count[k]);
    }


       }
   }

我知道要求别人帮我解决问题是不公平的。但任何类型的提示都会有所帮助。 谢谢

6 个答案:

答案 0 :(得分:1)

数字是否限制在0-9?如果是这样,我会很简单地完成任务。

(请注意您将输入分配给名为“input”的变量):

数[0] =输入; 计数[输入] ++;

此外,您可以在“0”中启动for循环,以避免在for循环之前进行分配。

只是一个提示。

希望这有帮助!

答案 1 :(得分:0)

理想的数据结构是HashMap

步骤: 1)初始化一个数组来存储数字和每个输入

2)检查带有密钥作为输入数字的hashmap条目是否已存在

3)如果存在则只增加其计数

4)否则使用key创建新条目作为数字并计为1

所以最后你的频率会被计算出来 如果你被迫使用2个阵列

1)初始化两个数组

2)对于每个输入循环数字数组并检查该数字是否已经在数组中

3)如果是这样的话,取数组索引并用相同的索引增加频率数组的值

4)如果不是freq [index] = 1

答案 2 :(得分:0)

这样做的正确方法是:

public Map<Integer, Integer> getFrequencies(Iterable<Integer> numbers) {
    Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>();
    for(Integer number : numbers) {
        if (frequencies.get(number) == null) {
            frequencies.put(number, 0);
        }
        frequencies.put(number, frequencies.get(number) + 1);
    }
    return frequencies;
}

返回地图number -> frequency

数组不是是Java的一种方式,应尽可能避免使用它们。请参阅Effective Java, Item 25: Prefer lists to arrays

答案 3 :(得分:0)

我删除了Scanner对象以更快地编写代码,只需将其替换为上面的代码即可。它可以正常工作。

    int[] numbers = { 1, 2, 2, 2, 3, 3, 3, 1, 1, 2 };
    int[] count = new int[10];
    int[] distinct = new int[10];

    count[0] = 1;
    distinct[0] = numbers[0];
    int disPos = 1; //Current possition in the distinct array
    boolean valueInarray = false;
    for (int i = 1; i < 10; i++) {
        valueInarray = false;
        for (int d = 0; d < i; d++) {

            if (numbers[i] == distinct[d]) {
                count[d] = count[d] + 1;
                valueInarray = true;
                break;
            }

        }
        if (!valueInarray) {
            distinct[disPos] = numbers[i];

            count[disPos] = 1;
            disPos++;
        }

    }

答案 4 :(得分:0)

如果你绝对必须使用数组..这是一种方法...

import java.util.Scanner;
import java.util.Arrays;

public class JavaApplication10 
{
  public static void main(String[] args)
  {
   int [] numbers = new int [10];
   int [] count = new int[10];
   int [] distinct = new int[10];
   int [] distinct1 = new int[1];
   int distinctCount = 0;
   boolean found = false;

   Scanner input = new Scanner(System.in);

   for (int i=0; i<10; i++) {
   found = false;
   System.out.print("Enter number " + i);
   numbers[i]=input.nextInt(); //Add input to numbers array

   for (int j=0; j<=distinctCount; j++)
   {
     if (distinct1[j] == numbers[i]){ // check to see if the number is already in the distinct array

           count[j] = count[j] + 1; // Increase count by 1
           found = true; 
           break;
     }
   }

   if (!found) {
       distinct[distinctCount] = numbers[i];
       count[distinctCount] = 1;
       distinctCount++;
       distinct1 = Arrays.copyOf(distinct, distinctCount+1);
   }

   }
   for (int j=0; j<distinctCount; j++) 
     System.out.println("The number " + distinct1[j] + " occurs " + count[j] + " times" );


}

}

答案 5 :(得分:0)

我认为这就是你需要的,如果我错了,请纠正我......

import java.util.HashMap;
import java.util.Scanner;

public class JavaApplication10 {
public static void main(String[] args) {
    // Initializing variables
    int[] numbers                       = new int[10];
    HashMap<Integer, Integer> table     = new HashMap<Integer, Integer>();
    Scanner input                       = new Scanner(System.in);

    // Getting the 10 inputs
    for(int x=0; x<10; x++) {

        // Asking for input
        System.out.println("Enter number "+x+":");
        numbers[x]=input.nextInt();

        // If the table contains the number, add 1
        // Otherwise: set value to 1
        if(table.containsKey(numbers[x]))
            table.put(numbers[x], table.get(numbers[x])+1);
        else
            table.put(numbers[x],1);

    }
    // Closing the reader
    input.close();      

    // Get the highest and smallest number
    int highest=0;
    int smallest=0;
    for(int i:table.keySet()) {
        if(i>highest)
            highest=i;
        if(i<smallest)
            smallest=i;
    }



    // For every value between the smallest and the highest
    for (int x=smallest; x<=highest; x++) {
        // Check if the frequency > 0, else continue
        if(table.get(x)==null)
            continue;
        // Output
        System.out.println(x+" is "+table.get(x)+" times in \'frequence\'");
    }

}
}

与其他代码不同,这也处理负数。如果您不想使用HashMaps,请告诉我,以便我可以使用数组创建内容。

让我知道它(不是)是否有效!
快乐的编码(祝你的任务好运);)-Charlie

相关问题