【问题标题】:How can I gets histogram using arrays in java?如何在java中使用数组获取直方图?
【发布时间】:2016-10-31 07:15:07
【问题描述】:

以数字形式从用户输入数组并显示直方图我错了代码在java中给出,用户可以输入5个数字输入,直方图应该以任何形式显示,没有限制

package p21;
import java.util.Scanner;

public class P21 {
 public static void main(String[] args) {
   {
    int count[] = new int[10]; // count array will keep elements of element
    // in particular range;
    int elements[]; // for example   27 15 34 22 11 11 19
    { // in above input there is count[0]=0;
     for (int i = 0; i < elements.length; i++) // count[1]=4 and count[2]=2 and count[3]=1;
     {
      if (elements[i] >= 0 && elements[i] < 50) {
       if (elements[i] < 10) {
        count[0]++;}
        else if (elements[i] >= 10 && elements[i] < 20) {
        count[1]++;}
        else if (elements[i] >= 20 && elements[i] < 30) {
        count[2]++;}
        else if (elements[i] >= 30 && elements[i] < 40) {
        count[3]++;}
        else {
        count[4]++;
       }}
       else if (elements[i] >= 50 && elements[i] <= 100) {
       if (elements[i] < 60) {
        count[5]++;}
        else if (elements[i] >= 60 && elements[i] < 70) {
        count[6]++;}
        else if (elements[i] >= 70 && elements[i] < 80) {
        count[7]++;}
        else if (elements[i] >= 80 && elements[i] < 90) {
        count[8]++;}
        else {
        count[9]++;

      }}}}

     {
     System.out.println("Histogram of the elements:");
     for (int i = 0; i < count.length; i++) // this loop will print line
     {
      for (int j = 0; j < count[i]; j++) // this will print elements element(*)
      { // at each line.
       System.out.print("* ");
      }
      if (count[i] != 0) // if line does'nt contain zero   
       System.out.println(""); // then if will change the row;
     }
    }
   }
   /*
   in above code if count[i]=zero means if there is elements
   element in particular range say [0-9] then it will
   elementst jump on next line;
   */
   {
    {
     Histogram hg = new Histogram();
     System.out.println("Enter the elements of Elements want in a  Histogram:");
     Scanner sc = new Scanner(System.in);

     int noOfElements = sc.nextInt();
     int histogramElements[] = new int[noOfElements];

     System.out.println("Enter the Elements for Histogram:");
     for (int i = 0; i < noOfElements; i++) {
      histogramElements[i] = sc.nextInt();
     }

     hg.showHistogram(histogramElements);
    }

【问题讨论】:

  • ..你不是问了一个类似的问题吗?请编辑代码格式并指定您遇到问题的确切位置。就像现在一样,由于大括号,代码甚至无法编译..
  • 你的牙套和对齐方式太奇怪了
  • @RajpootRajpoot 请修复您的大括号并重新调整您的代码。如果您希望有人花时间帮助您解决问题,您至少应该努力编写一个好问题并使您的代码易于理解
  • 用格式化程序修复了灾难性的大括号,尽管缩进需要一些工作。请不要发布不可读的代码。帮助你要困难得多。
  • @RajpootRajpoot .. 你的代码编译了吗?到处都是缺少和多余的大括号。

标签: java arrays optimization methods histogram


【解决方案1】:

对于您的分箱方法.....因为您只关心 10 组。将输入数字除以 10。绑定除后的结果,然后使用结果来索引您的计数器数组。

【讨论】:

  • 你有任何直方图算法吗?为什么你不尝试纠正我的代码。
猜你喜欢
  • 2011-01-17
  • 2011-05-08
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 2011-01-09
相关资源
最近更新 更多