【问题标题】:Creating a histogram within Java在 Java 中创建直方图
【发布时间】:2015-07-19 00:53:22
【问题描述】:

我用以下代码创建了一个直方图:

import java.util.*; 

public class Murray_A03Q3 {
    public static void main (String [] args){

        int num = 1;
        int[] nums = new int[10];  

        List<Integer> list = new ArrayList<Integer>();

        Scanner scan = new Scanner(System.in);


        while(num != 0){
           System.out.print("Enter a value to plot: ");
           num = scan.nextInt();
           System.out.println();
           if(num != 0)
              list.add(num);
        }


        for (int a = 0; a < list.size(); a++){
            nums[(list.get(a)-1) / 10]++;
        }

        for (int count = 0; count < 10; count++){
           System.out.print((count*1+1) + (count == 1 ? " ": " ") + "| \t");

           for(int h = 0; h < nums[count]; h++)
              System.out.print("#");

           System.out.println();
        }

    } // end of main

} // end of class Murray

输出为:

1 |     ######
2 |     
3 |     
4 |     
5 |     
6 |     #
7 |     
8 |     
9 |     #
10 | 

但我需要它来打印来自 1-10 而不是 1-100 的用户输入的值,就像它似乎正在打印的那样(上面的输出使用了十个之外的两个值,这就是为什么一个在 6 和 9 处打印的原因) .我已经扫描了代码并尝试以不同的方式操纵它以实际获得我想要的东西,但我无法弄清楚。我的问题是,实际上需要更改什么才能获得 1-10 之间的值?

感谢您的帮助!

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    你应该限制它,让他们只能输入 1-10。在您的 nums 数组中,我只存储了该索引号的出现次数。然后在打印时,只需打印 index+1,内部循环会为您执行 #。

    import java.util.*;
    
    public class Murray_A03Q3{
       public static void main (String [] args){
    
          int num = 1;
          int[] nums = new int[10];  
    
          List<Integer> list = new ArrayList<Integer>();
    
          Scanner scan = new Scanner(System.in);
    
          while(num != 0){
             System.out.print("Enter a value to plot: ");
             num = scan.nextInt();
             System.out.println();
             if(num > 0 && num <= 10)
                list.add(num);
             if(num > 10 || num < 0)
                    System.out.println("Please enter a number 1-10");
          }
    
          for (int a = 0; a < list.size(); a++){
             nums[a] = Collections.frequency(list, a);
          }
    
          for (int count = 0; count < 10; count++){
             System.out.print((count+1) + "\t|");
    
             for(int h = 0; h < nums[count]; h++)
                System.out.print("#");
    
             System.out.println();
          }
       } // end of main
    } // end of class Murray
    

    我还移动了选项卡前面的线以使其看起来更均匀。这是为 1、2、3 输入的。它用于在 1 行上显示所有 3 个#。

    1   |#
    2   |#
    3   |#
    4   |#
    5   |
    6   |
    7   |
    8   |
    9   |
    10  |
    

    【讨论】:

      【解决方案2】:

      如果您想排除 10 之外的内容,那么当您添加到 nums 时,您的循环是错误的。更改

      for (int a = 0; a < list.size(); a++){
          nums[(list.get(a)-1) / 10]++;
      }
      

      for (int a = 0; a < list.size(); a++) {
          if (list.get(a)-1 < 10) {
              nums[list.get(a)]++;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-17
        • 2022-01-21
        • 2021-02-19
        • 2017-01-29
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        相关资源
        最近更新 更多