如何记录整数的数组,以及重复的打印频率

时间:2012-04-07 23:06:54

标签: java arrays

注意:只是练习题,不适用于标记。

这是第一年Java课程中的练习题:

  

设计并实现一个应用程序,该应用程序读取用户在0到50(包括0和50)范围内的任意数量的整数,并计算每个整数的输入次数。处理完所有输入后,打印一次或多次输入的所有值(出现次数)。   此外,编写一个不返回任何值的方法,该值将计算用户输入的所有数字的平均值。

这就是我所拥有的(在我清理它之前,我已跳过“平均发生”部分):

import java.util.Scanner;
public class Main 
{       
public static Scanner scan = new Scanner(System.in);

    public static int[] userIntegers()      // this method will build the array of integers, stopping when an out-of-range input is given
    {
            System.out.println("Enter the number of integers to be recorded:  ");
            int numInts = scan.nextInt();

            int[] userArray = new int[numInts];
            int i = 0;
            while(i < numInts)
            {
                    System.out.println("Enter an integer between 1-50 inclusive:  ");
                    int userInteger = scan.nextInt();
                    if(isValidInteger(userInteger))
                    {
                            userArray[i] = userInteger;
                            i++;
                    }
                    else if(isValidInteger(userInteger) == false)
                    {
                            System.out.println("Try again.");
                    }                       
            }
            return userArray;
    }

    public static void occurrenceOutput(int[] input)         // this method will print the occurrence data for a given array
    {   
        int[] occurrenceArray = new int[51];

            int j = 0;
            while(j < 51)  // iterates through all integers from 0 to 50, while the integer in the array is equal to integer j, the corresponding occurance array element increments.
            {
                    for(int eachInteger : input)
                    {
                            occurrenceArray[j] = (eachInteger == j)? occurrenceArray[j]+=1:  occurrenceArray[j];
                    }
                    j++;
            }               

            int k = 0;
            for(int eachOccurrence : occurrenceArray) // as long as there is more than one occurrence, the information will be printed.
            {
                    if(eachOccurrence > 1)
                    {
                            System.out.println("The integer " + k + " occurrs " + eachOccurrence + " times.");
                    }
                    k++;
            }
    }

    public static boolean isValidInteger(int userInput)     // checks if a user input is between 0-50 inclusive        
    {        
        boolean validInt = (51 >= userInput && userInput >= 0)?  true:  false;
            return validInt;
    }

    public static void main(String[] args)
    {
        occurrenceOutput(userIntegers());
    }
}  

有人能指出我更优雅的方向吗?

编辑:谢谢你的帮助!这就是我现在所处的位置:

import java.util.Scanner;
public class simpleHist
{
    public static void main(String[] args)
{
            getUserInputAndPrint();
            getIntFreqAndPrint(intArray, numberOfInts);
    }

    private static int numberOfInts;
    private static int[] intArray;
    private static int[] intFreqArray = new int[51];

    public static void getUserInputAndPrint() 
    {
            //  The user is prompted to choose the number of integers to enter:
            Scanner input = new Scanner(System.in);
            System.out.println("Enter the number of Integers:  ");
            numberOfInts = input.nextInt();

            //  The array is filled withchInteger = integer; integers ranging from 0-50:
            intArray = new int[numberOfInts];
            int integer = 0;
            int i = 0;
            while(i < intArray.length)
            {
                    System.out.println("Enter integer value(s):  ");
                    integer = input.nextInt();
                    if(integer > 50 || integer < 0)
                    {
                            System.out.println("Invalid input.  Integer(s) must be between 0-50 (inclusive).");
                    }
                    else
                    {
                            intArray[i] = integer;
                            i++;
                    }
            }

            // Here the number of integers, as well as all the integers entered are printed:
            System.out.println("Integers: " + numberOfInts);    
            int j = 0;
            for(int eachInteger : intArray)
            {
                    System.out.println("Index[" + j + "] : " + eachInteger);
                    j++;
            }
    }

public static void getIntFreqAndPrint(int[] intArray, int numberOfInts)
{
    //  Frequency of each integer is assigned to its corresponding index of intFreqArray:
    for(int eachInt : intArray)
    {
        intFreqArray[eachInt]++;
    }

    //  Average frequency is calculated:
    int totalOccurrences = 0;
    for(int eachFreq : intFreqArray)
    {
        totalOccurrences += eachFreq;
    }
    double averageFrequency = totalOccurrences / numberOfInts;

    //  Integers occurring more than once are printed:
    for(int k = 0; k < intFreqArray.length; k++)
    {
        if(intFreqArray[k] > 1)
        {
            System.out.println("Integer " + k + " occurs " + intFreqArray[k] + " times.");
        }
            }

            //  Average occurrence of integers entered is printed:
            System.out.println("The average occurrence for integers entered is " + averageFrequency);
            }
    }

2 个答案:

答案 0 :(得分:2)

您实际上在寻找histogram。您可以使用Map<Integer,Integer>来实现它,或者由于元素范围限制为0-50,您可以使用包含51个元素[0-50]的数组,并在阅读时增加histogram[i] i

奖励:理解这个想法,你已经理解了count-sort

的基础知识

答案 1 :(得分:1)

要计算出现次数,您可以执行以下操作:

 for(int eachInteger : input) {
      occurrenceArray[eachInteger]++;
 }

这将取代你的while循环。