* 1.在这个list 的基础上,完成下列要求: * 1) 计算所有学生的平均年龄 * 2) 计算各个班级的平均分

 * 1.在这个list 的基础上,完成下列要求:
 * 1) 计算所有学生的平均年龄
 * 2) 计算各个班级的平均分
 * List<Student> list = new ArrayList<Student>();
 * 		list.add(new Student("Tom", 18, 100, "class05"));
 * 		list.add(new Student("Jerry", 22, 70, "class04"));
 * 		list.add(new Student("Owen", 25, 90, "class05"));
 * 		list.add(new Student("Jim", 30,80 , "class05"));
 * 		list.add(new Student("Steve", 28, 66, "class06"));
 * 		list.add(new Student("Kevin", 24, 100, "class04"));
 */
public class PracticeDemo01 {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<Student>();
  		list.add(new Student("Tom", 18, 100.0, "class05"));
  		list.add(new Student("Jerry", 22, 70.0, "class04"));
  		list.add(new Student("Owen", 25, 90.0, "class05"));
  		list.add(new Student("Jim", 30,80.0, "class05"));
  		list.add(new Student("Steve", 28, 66.0, "class06"));
  		list.add(new Student("Kevin", 24, 100.0, "class04"));

  		//年龄
		int sumAge = 0;
		//各个班级的总成绩
		double sum05 = 0.0;
		double sum04 = 0.0;
		double sum06 = 0.0;
		//统计各个班级的人数
		int count05 = 0;
		int count04 = 0;
		int count06 = 0;

  		//遍历list集合得到每一个学生
  		for(Student student : list){
  			//获取学生年龄
			Integer age = student.getAge();
			sumAge += age;
			//获取每个班级
			String classNum = student.getClassNum();
			if (classNum.equals("class05")){ //5班
				//获取成绩
				Double score = student.getScore();
				sum05 += score;
				count05++;
			}else if(classNum.equals("class04")){//4班
				Double score = student.getScore();
				sum04 += score;
				count04++;
			}else if(classNum.equals("class06")){//6班
				Double score = student.getScore();
				sum06 += score;
				count06++;
			}
		}
		//1) 计算所有学生的平均年龄
		System.out.println("所有学生的平均年龄:" + sumAge / list.size());
		// 2) 计算各个班级的平均分
		System.out.println("5班的平均分:" + sum05 / count05);
		System.out.println("4班的平均分:" + sum04 / count04);
		System.out.println("6班的平均分:" + sum06 / count06);






	}
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java多线程加速计算1*2*...*100的结果的示例代码: ```java public class MultiThreadCalculation { public static void main(String[] args) throws InterruptedException { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; long result = 1; int numOfThreads = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads); List<Future<Long>> futures = new ArrayList<>(); for (int i = 0; i < numOfThreads; i++) { int start = i * nums.length / numOfThreads; int end = (i + 1) * nums.length / numOfThreads; futures.add(executorService.submit(new CalculationTask(nums, start, end))); } for (Future<Long> future : futures) { result *= future.get(); } executorService.shutdown(); System.out.println("Result: " + result); } private static class CalculationTask implements Callable<Long> { private final int[] nums; private final int start; private final int end; public CalculationTask(int[] nums, int start, int end) { this.nums = nums; this.start = start; this.end = end; } @Override public Long call() throws Exception { long result = 1; for (int i = start; i < end; i++) { result *= nums[i]; } return result; } } } ``` 在上面的示例中,我们使用了Java的Executor框架来创建了一个线程池,然后将计算任务分配给不同的线程进行并行计算。每个线程计算完成后,将其结果保存在一个Future对象中,最后将所有Future对象的结果相乘得到最终结果。 由于本例中计算任务比较简单,所以使用多线程并不会带来很大的性能提升,但是对于更复杂的计算任务,多线程可以大大提高计算效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值