创建垂直直方图

时间:2017-06-19 01:22:02

标签: java histogram

基本上我一直试图让我的直方图显示星号垂直对齐在它上面增加的字母上方。我一直试图找出最有效的方法让星号在字母重复之上对齐。有什么建议吗?

    **My current output displays this horizontally**

asfklafjasjfk
A (3) ***
F (3) ***
J (2) **
K (2) **
L (1) *
S (2) **
ABCDEFGHIJKLMNOPQRSTUVWXYZ

我希望它显示此

  abcaaaabbzzzzz    
                           *  
  *                        *
  **                       *
  **                       *
  ***                      *
  ABCDEFGHIJKLMNOPQRSTUVWXYZ

我在下面列出了我的代码

public class histogram {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String lettersInput = input.nextLine();
        lettersInput=lettersInput.toUpperCase();
        String map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int[] count = new int[map.length()];

        for(int x = 0; x <  lettersInput.length();x++){

            int letter = map.indexOf(lettersInput.charAt(x));

            if(letter < 0){

                continue;
            }

            count[letter]++;
        }
        for(int x = 0; x < count.length; x++){

            if(count[x]< 1)
                continue;
            System.out.println(String.format("%s (%d) %s",

                    map.charAt(x),

                    count[x],

                    new String(new char[count[x]]).replace('\0','*')));
        }

        System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
}

1 个答案:

答案 0 :(得分:0)

我采用的方法是使用有序地图,其键是字母表的字母,其值是每个字母的出现次数。只需遍历输入字符串即可填充地图。然后,迭代每行可能的输出并打印出每个字母的空格或星号。我在地图的值上使用TreeMap<Character, Integer> map = new TreeMap<>(); String input = "abcaaaabbzzzzz".toUpperCase(); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i=0; i < input.length(); ++i) { Integer val = map.get(input.charAt(i)); map.put(input.charAt(i), val == null ? 1 : val + 1); } Collection<Integer> c = map.values(); int maxFrequency = Collections.max(c); System.out.println("Input:\n" + input); for (int i=maxFrequency; i > 0; --i) { for (int j=0; j < alphabet.length(); ++j) { Integer count = map.get(alphabet.charAt(j)); System.out.print((count != null && count >= i) ? "*" : " "); } System.out.println(); } System.out.println(alphabet); 来查找直方图的高度。

Input:
ABCAAAABBZZZZZ
*                        *
*                        *
**                       *
**                       *
***                      *
ABCDEFGHIJKLMNOPQRSTUVWXYZ

<强>输出:

public <T> T execute(ConnectionCallback<T> action) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");

    Connection con = DataSourceUtils.getConnection(getDataSource());
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null) {
            // Extract native JDBC Connection, castable to OracleConnection or the like.
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        else {
            // Create close-suppressing Connection proxy, also preparing returned Statements.
            conToUse = createConnectionProxy(con);
        }
        return action.doInConnection(conToUse);
    }
    catch (SQLException ex) {
        // Release Connection early, to avoid potential connection pool deadlock
        // in the case when the exception translator hasn't been initialized yet.
        DataSourceUtils.releaseConnection(con, getDataSource());
        con = null;
        throw getExceptionTranslator().translate("ConnectionCallback", getSql(action), ex);
    }
    finally {
        DataSourceUtils.releaseConnection(con, getDataSource());
    }
}

在这里演示:

Rextester