展开全部
package image;
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 list = new ArrayList();
Stu stu0 = new Stu("张三", "语文", 60);
Stu stu1 = new Stu("张三", "数学", 70);
Stu stu3 = new Stu("李四e5a48de588b63231313335323631343130323136353331333363356564", "语文", 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