输出99乘法口诀表
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MultiplicationTable { public static void main(String[] args) { // 定义字符串缓存,用来存放乘积结果 StringBuffer sb = new StringBuffer(); // 输出乘法口诀表 for (int i=1; i<=9; i++) { for (int j=1; j<=i; j++) { // 做对齐调整,从第二列开始,若结果长度为个位数,则在结果前面补空格 if (j ==2 && String.valueOf(i*j).length() == 1) { System.out.print(j + "*" + i + "= " + i*j + " "); // 将乘积存放在sb中 sb.append(i*j); } else { System.out.print(j + "*" + i + "=" + i*j + " "); sb.append(i*j); } } // 一行结束,进行换行操作 System.out.println(); } // 定义map,存放字符和其出现的次数 Map<Character, Integer> map = new HashMap<Character, Integer>(); String str = sb.toString(); // 遍历str,将字符及其出现的次数存放入map for (int i=0; i<str.length(); i++) { Character num = str.charAt(i); if (null != map.get(num)) { map.put(num, map.get(num) + 1); } else { map.put(num, 1); } } // 获取entry结果集,并遍历输出 Set<Entry<Character, Integer>> entrySet = map.entrySet(); Iterator<Entry<Character, Integer>> it = entrySet.iterator(); while (it.hasNext()) { Entry<Character, Integer> entry = it.next(); System.out.println(entry.getKey() + " " + entry.getValue()); } } }
输出9乘9乘法口诀表并实现0-9每个数字出现的次数并打印出来
原文地址:http://blog.csdn.net/magi1201/article/details/45344951