Java学生成绩统计

学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:

         1.进行学生成绩的随机生成, 区间为 [50, 100].
         2.找出成绩最好、最差的同学。但有挂科的同学不参加评比.
10.1 实际代码中,for 和 if 是最常见的, switch 和 while 使用少得多.
10.2 使用了 continue, 它是指继续跳过本次循环后面的代码,直接进入下一次循环. 而 break 是跳出整个循环体.
10.3 为了随机数,迫不得已提前使用了 new 语句生成对象.
10.4 通过数据测试找出程序中的 bug.

package basic;
import java.util.*;
public class student {

	public static void main(String args[]) {
		task1();
	}
	//task 函数
	public static void task1() {
		int n = 10;
		int m = 3;
		int lowerBound = 50;
		int upperBound = 100;
		int threshold = 60;
		
		//注意生成随机数的方法
		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] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);
			}
			}
		System.out.println("The data is:"+ 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("The total Scores are:" + Arrays.toString(totalScores));
		
		//找出最好和最坏的学生
		int tempBestIndex = -1;
		int tempWorstIndex = -1;
		int tempBestScore = 0;
		int tempWorstScore = m*upperBound+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("Can not find the best student. All have failed");
		}
		else {
			System.out.println("The best student is NO." + tempBestIndex + " with Score:"+Arrays.toString(data[tempBestIndex]));
			
		}
		if(tempWorstIndex == -1) {
			System.out.println("Can not find the worst student. All student hava failed");
		}
		else {
			System.out.println("The worst student is NO." + tempWorstIndex + " with Score:"+Arrays.toString(data[tempWorstIndex]));
			
		}
	}	
}

我的感受:

第一章节

已经学完了,知识上没有多大问题。我的主要不足在于所知道的类太少了,而且代码书写很不规范。基础花不了多少时间,但是一定要牢靠,形成统一。

以后的学习中我会额外拓展知识面,基础规范慢慢抓。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值