从Java中的char [] []打印图形 [英] Printing a Graph from a char[][] in Java

查看:80
本文介绍了从Java中的char [] []打印图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的方法遍历给定的 int [] ,将数组中找到的数字的频率以及每个出现的不同数字都存储到 HashMap

The method below loops through a given int[], storing both the frequencies of the numbers found in the array along with each different appearing number into a HashMap.

循环后, HashMap 看起来像这样( num = frequency):

After looping, the HashMap would look something like this (num=frequency):

{0=4, 656=1, 1=1, 2=2, 3=2, 4=3, 8=1, 56=1, 75=1, 12=3, 13=1, 61=1}

此后,该方法创建一个 char [] [] ,表示指定的x& y坐标表示每个整数的大小和频率。 x轴表示数字的大小,y轴表示 HashMap 中数字的频率。

Following this, the method creates a char[][], representing designated x & y coords for the size and frequency of each integer. The x axis represents the size of the number, and the y axis represents the number's frequency in the HashMap.

private static void print(int[] intArr) {
    /*
   * Purpose of method is to print:
   * The frequency of each index
   * A graphical histogram of the frequencies of each index
    */
    Map<Integer, Integer> map = new HashMap<>();
    for (int i : intArr)
        // If current index of the array being searched is found in map, +1 to total # of occurrences of that index
        if (map.containsKey(i)) map.put(i, map.get(i) + 1);
        else map.put(i, 1);

    // Iterate through all entries of map, printing the key and value of each
    for (Entry<Integer, Integer> entry : map.entrySet()) System.out.println("Frequency of index " + entry.getKey() + " is " + entry.getValue());
    System.out.println();

    char[][] graph = new char[Collections.max(map.values()) + 1][map.keySet().size() + 1];
    for (int x = 0; x < graph.length; x++)
        for (int y = 0; y < graph[x].length; y++) {
            graph[x][y] = ' ';
            // if (....) graph[x][y] = '*';
            // else graph[x][y] = ' ';
        }
    // Create borders
    for (int i = 0; i < graph.length; i++) graph[i][0] = '|';
    graph[graph.length - 1][0] = '+';
    for (int i = 1; i < graph[0].length; i++) graph[graph.length - 1][i] = '-';
    // Print graph into console
    for (int x = 0; x < graph.length; x++) {
        for (int y = 0; y < graph[0].length; y++) System.out.print(graph[x][y]);
        System.out.println();
    }
}

代码的输出如下:

Frequency of index 0 is 4
Frequency of index 656 is 1
Frequency of index 1 is 1
Frequency of index 2 is 2
Frequency of index 3 is 2
Frequency of index 4 is 3
Frequency of index 8 is 1
Frequency of index 56 is 1
Frequency of index 75 is 1
Frequency of index 12 is 3
Frequency of index 13 is 1
Frequency of index 61 is 1

|            
|            
|            
|            
+------------

现在问题我我似乎无法弄清楚如何在相应的x&上画一个星号。 y协调需要它们的地方。我希望该图看起来像这样:

Now the problem I'm having is that I can't seem to figure out how to plot an asterisk at the corresponding x & y coords where they are needed. I want the graph to look like this instead:

|*           
|     *   *  
|   **       
| **   *** **
+------------

我需要的是一个正确的if语句,该语句检查当前打印的坐标是否是正确的列和行的坐标,如果不是,则打印一个空白区域。截至目前,该程序所做的全部工作就是用空白填充所有内容。我已经尝试过弄乱代码很多,但是它大多只是以错误和 ArrayIndexOutOfBounds 异常结束。

What I need is a proper if statement checking if the current coordinate being printed is that of the proper column and row, and if not, then print an empty space. As of now, all the program does is fill in everything with empty spaces. I've tried messing around with the code a lot, but it mostly just ended with bugs and ArrayIndexOutOfBounds exceptions.

推荐答案

应在打印之前插入类似的内容:

Should work by inserting something like this before printing:

for (Entry<Integer, Integer> entry : map.entrySet()) {
  int index = entry.getKey();
  int frequency = entry.getValue();
  if (frequency < graph.length && index < graph[frequency].length) {
    graph[frequency][index] = '*'; 
  }
}

如果您确实希望在设置循环中使用此命令(我不会这样做,因为它似乎不太可读),您可以执行以下操作:

If you really want this in the setup loop (which I wouldn't do because it seems less readable), you can do something like this:

for (int x = 0; x < graph.length; x++) {
  Integer freq = map.get(x);
  int f = freq == null ? 0 : freq;
  for (int y = 0; y < graph[x].length; y++) {
     graph[x][y] = y == f ? '*' : ' ';
  }
}

OOB异常的原因可能是该图数组小于您的数字;典型原因:

The reason for your OOB exceptions is probably that the graph array is smaller than your numbers; typical reasons:


  • 一个错误

  • 坐标混合

  • 数组大小不足

请注意,当打印看起来比镜像容易时,代码片段不会镜像y坐标

Note that the code fragments don't mirror the y coordinates as doing so when printing seems easier than mirroring them in all other places.

编辑
要固定大小,请将代码更改为

Edit: To fix the size, change the code to

char[][] graph = new char[Collections.max(map.values()) + 1]
                         [Collections.max(map.keySet()) + 1];

或者在较早的循环中跟踪大小。

Or keep track of the size in ine of the earlier loops.

这篇关于从Java中的char [] []打印图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆