黑马程序员技术交流社区
标题:
点招可能性题
[打印本页]
作者:
流空有痕
时间:
2016-9-13 23:59
标题:
点招可能性题
打印出字符串"abbbbbccccdddee"中每个字符出现的次数,要求输出格式:"a(1)b(5)c(4)d(3)e(2)",并将结果写入文件。
public class Test02 {
public static void main(String[] args) throws IOException {
// 1,定义一个需要被统计字符的字符串
String s = "abbbbbccccdddee";
// 2,将字符串转换为字符数组
char[] arr = s.toCharArray();
// 3,定义双列集合,存储字符串中字符以及字符出现的次数
TreeMap<Character, Integer> map = new TreeMap<>();
// 4,遍历字符数组获取每一个字符,并将字符存储在双列集合中
for (char c : arr) {
// 5,存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
if (!map.containsKey(c)) { // 如果不包含这个键
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
// 6,遍历集合将键和值拼接起来
StringBuilder sb = new StringBuilder();
for (Character key : map.keySet()) { // hm.keySet()代表所有键的集合
sb.append(key + "(" + map.get(key) + ")");
}
// 输出结果
System.out.println(sb);
// 创建输出流对象写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
bw.write(sb.toString());
bw.close();
}
}
作者:
Nightwish丶
时间:
2016-9-15 23:06
做了下,几分钟做好后看了答案,我晕了
public class iii {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//接收
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个小数");
double b = sc.nextDouble();
System.out.println(swap(b));
}
public static int swap(double b) {
double nb=b+0.5;
int a= (int) ((nb/2)*2);
return a;
}
}
作者:
lhclannad
时间:
2016-9-15 23:08
这不是课堂上讲过的题目吗
作者:
许华洋
时间:
2016-9-16 00:14
赞!!!!!!!!!!!!
作者:
天天图蓝
时间:
2016-9-16 00:17
常规题,挺简单的啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2