java中list集合求学生总成绩,求张三和李四的总成绩,用循环判断的形式实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中list集合求学生总成绩,求张三和李四的总成绩,用循环判断的形式实现相关的知识,希望对你有一定的参考价值。
public class Stu
public String name;
public String course;
public int score;
public int total;
public Stu(String name,String course,int score)
this.name=name;
this.course=course;
this.score=score;
public String getName()
return name;
public int getScore()
return score;
public int getTotal()
return total;
public void setTotal(int total)
this.total=total;
public static void main(String[] args)
List<Stu> list=new ArrayList<Stu>();
Stu stu0=new Stu("张三","语文",60);
Stu stu1=new Stu("张三","数学",70);
Stu stu3=new Stu("李四","语文",65);
Stu stu4=new Stu("李四","数学",75);
list.add(stu0);
list.add(stu1);
list.add(stu3);
list.add(stu4);
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Stu
public String name;
public String course;
public int score;
public int total;
public Stu(String name, String course, int score)
this.name = name;
this.course = course;
this.score = score;
public String getName()
return name;
public int getScore()
return score;
public int getTotal()
return total;
public void setTotal(int total)
this.total = total;
public static void main(String[] args)
List<Stu> list = new ArrayList<Stu>();
Stu stu0 = new Stu("张三", "语文", 60);
Stu stu1 = new Stu("张三", "数学", 70);
Stu stu3 = new Stu("李四", "语文", 65);
Stu stu4 = new Stu("李四", "数学", 75);
list.add(stu0);
list.add(stu1);
list.add(stu3);
list.add(stu4);
// 总成绩
BigDecimal totalB = new BigDecimal("0");
BigDecimal totalZhangsan = new BigDecimal("0");
BigDecimal totalLisi = new BigDecimal("0");
for (Stu stu : list)
totalB = totalB.add(new BigDecimal(stu.getScore() + ""));
if ("张三".equals(stu.getName()))
totalZhangsan = totalZhangsan.add(new BigDecimal(stu.getScore() + ""));
else if ("李四".equals(stu.getName()))
totalLisi = totalLisi.add(new BigDecimal(stu.getScore() + ""));
System.out.println(totalB.doubleValue());
System.out.println(totalZhangsan.doubleValue());
System.out.println(totalLisi.doubleValue());
使用BigDecimal避免分数为小数时失真
结果为:
总成绩:270.0
张三:130.0
李四:140.0
List<Stu> list = new ArrayList<Stu>();
Stu stu0=new Stu("张三","语文",60);
Stu stu1=new Stu("张三","数学",70);
Stu stu3=new Stu("李四","语文",65);
Stu stu4=new Stu("李四","数学",75);
list.add(stu0);
list.add(stu1);
list.add(stu3);
list.add(stu4);
int sumScore = 0;//声明变量保存学生总成绩
int zhangsanSumScore = 0;//声明变量保存张三总成绩
int lisiSumScore = 0;//声明变量保存李四总成绩
for (Stu stu : list) //循环list
sumScore = sumScore + stu.getScore();//计算总成绩
if( stu.getName().equals("张三") )//如果姓名是张三,则计算成绩
zhangsanSumScore = zhangsanSumScore + stu.getScore();
else//否则就是李四的成绩
lisiSumScore = lisiSumScore + stu.getScore();
System.out.println("学生总成绩为"+sumScore);
System.out.println("张三总成绩为"+zhangsanSumScore);
System.out.println("李四总成绩为"+lisiSumScore);
参考技术B //使用map存储总成绩,key:学生姓名 value:学生成绩
Map<String,Integer> map = new HashMap<String,Integer>();
for(Stu stu:list)
if(!map.containsKey(stu.getName))
map.put(stu.getName,stu.getScore());
else
map.put(stu.getName,map.get(stu.getName)+stu.getScore());
System.out.println(map);
asp:查询每个学生的各科成绩,总分,平均分的sql语句
只有一个score表,字段是:xjh name zuohao xueke score
L001 张三 01 语文 65
L002 李四 02 数学 85
L001 张三 01 数学 75
L002 李四 02 语文 55
我想要的结果是: xjh name zuohao yuwen shuxue zongfen pjf
L001 张三 01 65 75 140 70
L002 李四 02 55 85 140 70
下面是我的sql语句:
dim rs,sql,sql2,zt,ck,nf,xq,cx,bj,bjmc,info
nf="2016"
xq="春季学期"
cx="期中考试"
bjmc="小学2012级3班"
set rs=server.CreateObject("Adodb.Recordset")
sql="select zuohao,name,xjh,"
sql=sql&"sum(case when xueke='语文' then score else 0 end) as yuwen,"
sql=sql&"sum(case when xueke='数学' then score else 0 end) as shuxue,"
sql=sql&"sum(case when xueke='英语' then score else 0 end) as yingyu,"
sql=sql&"sum(case when xueke='综合' then score else 0 end) as zhonghe,"
sql=sql&"sum(score) as zongfen,"
sql=sql&"sum(score)*1.0/4 as pjf"
sql=sql&" from score where nianfen='"&nf&"' and xueqi='"&xq&"' and cixu='"&cx&"' and bjmc='"&bjmc&"' group by zuohao,name,xjh order by desc zongfen"
rs.open sql,conn,1,1
提示错误:错误 '80004005' ,/StudentScore.asp,行 92 (注:92行就是:rs.open sql,conn,1,1)
怎么办啊?谢谢大虾
nf="2016"
xq="春季学期"
cx="期中考试"
bjmc="小学2012级3班"
set rs=server.CreateObject("Adodb.Recordset")
sql="select zuohao,name,xjh,"
sql=sql&"sum(case when xueke='语文' then score else 0 end) as yuwen,"
sql=sql&"sum(case when xueke='数学' then score else 0 end) as shuxue,"
sql=sql&"sum(case when xueke='英语' then score else 0 end) as yingyu,"
sql=sql&"sum(case when xueke='综合' then score else 0 end) as zhonghe,"
sql=sql&"sum(score) as zongfen,"
sql=sql&"sum(score)*1.0/4 as pjf"
sql=sql&" from score where nianfen='"&nf&"' and xueqi='"&xq&"' and cixu='"&cx&"' and bjmc='"&bjmc&"' group by zuohao,name,xjh order by zongfen desc "
rs.open sql,conn,1,1追问
错在哪个地方哦?没看出来,先谢过了
追答order by desc zongfen 写错了
-->order by zongfen desc
我改过来了,还是提示同样的错误代码
以上是关于java中list集合求学生总成绩,求张三和李四的总成绩,用循环判断的形式实现的主要内容,如果未能解决你的问题,请参考以下文章