java学习第10天,完成一个小任务

这是对之前java学习代码的一个总结,应用了之前所学的知识。需要好好理解融会贯通。

package java1to10;

import java.util.Arrays;
import java.util.Random;

public class D10_Task1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		task1();
	}

	public static void task1() {
		int n = 10;
		int m = 3;
		int lowBound = 30;
		int upBound = 80;
		int threshold = 40;

		Random tempRandom = new Random();// 产生一个随机数
		int[][] data = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				data[i][j] = lowBound + tempRandom.nextInt(upBound - lowBound);
			}
		}
		System.out.println("成绩是:\r\n" + Arrays.deepToString(data));
		int[] totalScores = new int[n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (data[i][j] < threshold) {
					totalScores[i] = 0;
					break;
				}
				totalScores[i] += data[i][j];
			}
		}
		System.out.println("总成绩是:\r\n" + Arrays.toString(totalScores));
		int tempBestIndex = 1;
		int tempWorstIndex = 1;
		int tempBestScore = 2;
		int tempWorstScore = m * upBound + 1;
		for (int i = 0; i < n; i++) {
			if (totalScores[i] == 0) {
				continue;
			}
			if (tempBestScore < totalScores[i]) {
				tempBestScore = totalScores[i];
				tempBestIndex = i;
			}
			if (tempWorstScore > totalScores[i]) {
				tempWorstScore = totalScores[i];
				tempWorstIndex = i;
			}
		}
		if (tempBestIndex == -1) {
			System.out.println("没有发现成绩优秀的学生。");
		} else {
			System.out.println(String.format("第%s个学生,成绩优异,成绩是:%s", tempBestIndex, Arrays.toString(data[tempBestIndex])));
		}
		if (tempWorstIndex == -1) {
			System.out.println("也没有发现特别差的学生。");
		} else {
			System.out.println(String.format("第%s个学生,成绩较差,成绩是:%s", tempWorstIndex, Arrays.toString(data[tempWorstIndex])));
		}
	}
}

输出结果:

成绩是:
[[33, 37, 37], [70, 48, 51], [77, 53, 62], [68, 56, 40], [36, 54, 65], [52, 63, 63], [63, 37, 34], [54, 46, 67], [60, 50, 44], [77, 30, 48]]
总成绩是:
[0, 169, 192, 164, 0, 178, 0, 167, 154, 0]2个学生,成绩优异,成绩是:[77, 53, 62]8个学生,成绩较差,成绩是:[60, 50, 44]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值