集册 Java实例教程 计算频率

计算频率

欢马劈雪     最近更新时间:2020-01-02 10:19:05

655
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算频率

public class Main 

{

   public static void main(String[] args)/*时代Java - nowjava.com 提 供*/

   {

      // student response array (more typically, input at run time)

      int[] responses = {1, 3, 5, 4, 3, 5, 2, 1, 3, 3, 3, 4, 3, 3, 3, 

         2, 3, 3, 2, 14};

      int[] frequency = new int[6]; // array of frequency counters


      for (int answer = 0; answer < responses.length; answer++)

      {

         try

         {

            ++frequency[responses[answer]];

         } 

         catch (ArrayIndexOutOfBoundsException e)

         {/*时   代     Java  公  众  号 - nowjava.com 提 供*/

            System.out.println(e); // invokes toString method

            System.out.printf("   responses[%d] = %d%n%n", 

               answer, responses[answer]);

         } 

      } 


      System.out.printf("%s%10s%n", "Rating", "Frequency");

   

      
展开阅读全文