2012-04-07 25 views
2

注意:只是练习题,不适用于标记。如何记录整数的列数和打印副本的频率

这是在第一年的Java课程给予了实践问题:

设计和实现一个应用程序,用户读取整数任意数量的,是范围为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

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

奖励:理解这个想法,你明白的count-sort

+0

哇,我们现在完全是我的离散课程中的直方图,甚至没有点击。计数排序的链接非常有帮助,谢谢! – 2012-04-08 08:00:36

+0

@obfuscater:我很高兴你正在建立这种联系。你很快就会意识到,作为程序员,你正在学习的数学和理论课程对你来说将是一笔宝贵的财富! – amit 2012-04-08 13:51:16

1

要计算出现次数的基础知识,你可以做这样的事情:

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

这将取代while循环。

+0

不要回答问题标签homewotk – amit 2012-04-07 23:13:25

+0

Amit:对不起,我完全错过了作业标签。 – 2012-04-08 00:24:38

+0

我没有想过以这种方式增加数组元素,(只有在数组上一个星期)。比我所拥有的眼睛容易得多! – 2012-04-08 08:02:42