java程序设计求详解
发布网友
发布时间:2022-04-29 04:30
我来回答
共3个回答
热心网友
时间:2023-05-05 23:51
package com.hundsun.fmbp.crk.test.day1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description TODO
* @Author Becolette
* @DateTime 2019-9-30 下午4:43:10
* @Modified By Becolette At 2019-9-30 Description TODO
*/
public class Test1 {
public static void main(String[] args) throws IOException {
List<Map<String, Object>> voteList = getVoteList();
int totalNum = 0;
InputStreamReader reader = null;
try {
System.out.println("输入人员名称并按Enter开始统计,如果结束统计输入end:");
while (true) {
// 从键盘接收数据
reader = new InputStreamReader(System.in);
String input = new BufferedReader(reader).readLine();
Boolean flag = false;
if ("end".equals(input)) {
System.out.println(voteList);
break;
} else {
totalNum++;
for (Map<String, Object> voteMap : voteList) {
if (input.equals(voteMap.get("name"))) {
voteMap.put("p_num", Integer.parseInt(voteMap.get("p_num").toString()) + 1);
flag = true;
}
}
if (!flag) {
System.out.println("输入人员名称不匹配!");
totalNum--;
}
for (Map<String, Object> voteMap : voteList) {
if (totalNum != 0) {
voteMap.put("p_rate", Double.parseDouble(voteMap.get("p_num").toString()) / totalNum * 100 + "%");
}
}
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
}
/**
* @Description 封装一个集合存储数据
* @Author Becolette
* @DateTime 2019-9-30 下午5:35:27
* @Modified By Becolette At 2019-9-30 Description TODO
*/
public static List<Map<String, Object>> getVoteList() {
// 可用map封装
List<Map<String, Object>> voteList = new java.util.ArrayList<Map<String, Object>>();
Map<String, Object> voteMap1 = new HashMap<String, Object>();
voteMap1.put("name", "A");
voteList.add(voteMap1);
Map<String, Object> voteMap2 = new HashMap<String, Object>();
voteMap2.put("name", "B");
voteList.add(voteMap2);
Map<String, Object> voteMap3 = new HashMap<String, Object>();
voteMap3.put("name", "C");
voteList.add(voteMap3);
Map<String, Object> voteMap4 = new HashMap<String, Object>();
voteMap4.put("name", "D");
voteList.add(voteMap4);
Map<String, Object> voteMap5 = new HashMap<String, Object>();
voteMap5.put("name", "E");
voteList.add(voteMap5);
for (Map<String, Object> voteMap : voteList) {
voteMap.put("p_num", 0);
voteMap.put("p_rate", 0.0d);
}
return voteList;
}
}
热心网友
时间:2023-05-05 23:51
放到一个数组或者集合
里面,具体的得票百分
比是通过计算出来的,
把所有人的票数加起来
就知道了总票数,然后
就简单了,数学公式一
算就出来了,排名吗,
来一个排序,就ok了
热心网友
时间:2023-05-05 23:52
这是考察Java集合的,用java8新特性很快就能搞定,几行代码的事