java统计分数_Java集合的应用(单词和分数的统计)

1.统计每个单词出现的次数 :

思路:第一次创建容器,并且放入值,第二次使用容器存放对应的值即可。

实体类

public class Letter {

private String name; //单词的名字

private int count; //单词出现的次数

public Letter(){

}

public Letter(String name,int count){

this.name=name;

this.count=count;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

}

实现方法

/**

* 处理思路:第一次创建容器,并且放入值

* 第二次使用容器存放对应的值即可

*/

public static void method(Map map, String str) {

String[] strings = str.split(" ");

for (int i = 0; i < strings.length; i++) {

if (!map.containsKey(strings[i])) {

map.put(strings[i], new Letter(strings[i], 1)); //第一次创建容器,并且放入值

} else {

//在容器中存放值

Letter letter = map.get(strings[i]);

letter.setCount(letter.getCount() + 1);

}

}

}

测试方法

public static void main(String[] args) {

Map map = new HashMap();

String str = "this is a cat and that is a mice and where is the food";

method(map, str);

Set> entries = map.entrySet();

for (Map.Entry entry : entries) {

System.out.println("单词:" + entry.getKey() + ",出现" + entry.getValue().getCount() + "次数");

}

}

2.统计班级的总分数

实体类 :Student

public class Student {

private String name;

private String stNo;

private int score;

public Student() {

}

public Student(String name, String stNo, int score) {

this.name = name;

this.stNo = stNo;

this.score = score;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getStNo() {

return stNo;

}

public void setStNo(String stNo) {

this.stNo = stNo;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

}

ClassRoom

public class ClassRoom {

private String stNo;

private List studentList;

private int totalScore;

public ClassRoom() {

}

public ClassRoom(String stNo,int totalScore){

this.stNo=stNo;

this.totalScore=totalScore;

}

public ClassRoom(String stNo, List studentList, int totalScore) {

this.stNo = stNo;

this.studentList = studentList;

this.totalScore = totalScore;

}

public String getStNo() {

return stNo;

}

public void setStNo(String stNo) {

this.stNo = stNo;

}

public List getStudentList() {

return studentList;

}

public void setStudentList(List studentList) {

this.studentList = studentList;

}

public int getTotalScore() {

return totalScore;

}

public void setTotalScore(int totalScore) {

this.totalScore = totalScore;

}

}

实现的方法

/**

* 统计班级的分数

* 分析思路:第一次创建容器并且放入值,第二次直接向容器中存放值即可

*/

public static void tjScore(Map map,List studentList){

for (Student student:studentList){

String stNo= student.getStNo();

if (!map.containsKey(stNo)){

map.put(stNo,new ClassRoom(stNo,student.getScore()));

}else{

ClassRoom classRoom = map.get(stNo);

classRoom.setTotalScore(classRoom.getTotalScore()+student.getScore());

}

}

}

测试的方法

public static void main(String[] args) {

//存放学生

List studentList=new ArrayList();

studentList.add(new Student("aa","001",80));

studentList.add(new Student("bb","001",80));

studentList.add(new Student("cc","002",80));

studentList.add(new Student("dd","002",80));

studentList.add(new Student("ff","003",80));

studentList.add(new Student("ee","003",80));

Map map=new HashMap();

tjScore(map,studentList);

Set> entries = map.entrySet();

for (Map.Entry entry:entries){

System.out.println("班级:"+entry.getKey()+",的总分:"+entry.getValue().getTotalScore());

}

}

每天进步一丢丢

完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值