网站首页 精选百科 > 正文
大家好,小耶来为大家解答以上的问题。java 调用dll,java 面向对象编程这个很多人还不知道,现在让我们一起来看看吧!
1、一共三个类:ScoreArray.java、StudentScoreArray.java和Test1.java,具体为:public class ScoreArray {private int[] scores;private int scoreCount;public int[] getScores() {return scores;}public int getScoreCount() {return scoreCount;}//构造函数public ScoreArray(int[] scores) {this.scores = scores;for (int score : scores) {if (score >= 0 && score <= 100) {this.scoreCount++;}}}//求最大值public int getMax() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[scores.length - 1];}//求最小值public int getMin() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[0];}//求均值public double getAvg() {int sum = 0;for (int score : this.scores) {sum += score;}return new BigDecimal(sum).divide(new BigDecimal(this.scores.length),2, BigDecimal.ROUND_HALF_UP).doubleValue();}//排序public void sort() {int temp;for (int i = 0; i < this.scores.length; i++) {for (int j = 0; j < this.scores.length - 1 - i; j++) {if (this.scores[j] > this.scores[j + 1]) {temp = this.scores[j];this.scores[j] = this.scores[j + 1];this.scores[j + 1] = temp;}}}}//静态说明类public static void explain() {System.out.println("本类[ScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");}}public class StudentScoreArray extends ScoreArray {public StudentScoreArray(int[] scores) {super(scores);}//统计public void statistic() {super.sort();Map
2、另外需要遍历这个map实现统计结果的打印,我这里使用了java8以后才支持的Lambda表达式,所以你要运行这个程序必须要使用jdk1.8以上的版本。
3、如果你觉得这样不妥,可以网上再搜一下map的遍历方式。
4、运行结果:啊 代码又一坨的的挤在一起了,百度知道这个真是无语,我把几个关键的地方截个图给你康康:ScoreArray.java成员变量和构造函数忘了说了,构造的同时还统计了有效分数(0~100)的数量求最大值冒泡排序后取最大值求最小值冒泡排序后取最小值求均值利用java 的BigDecimal类解决除法运算精度的问题,四舍五入并保留了两位小数排序就是冒泡排序,从小到大静态的文本说明StudentScoreArray.java:继承分数分布统计注意我说的map那里静态说明文本Test1.java:测试用例都使用的数组:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};一共三个类:ScoreArray.java、StudentScoreArray.java和Test1.java,具体为:public class ScoreArray {private int[] scores;private int scoreCount;public int[] getScores() {return scores;}public int getScoreCount() {return scoreCount;}//构造函数public ScoreArray(int[] scores) {this.scores = scores;for (int score : scores) {if (score >= 0 && score <= 100) {this.scoreCount++;}}}//求最大值public int getMax() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[scores.length - 1];}//求最小值public int getMin() {int[] scores = this.scores;int temp;for (int i = 0; i < scores.length; i++) {for (int j = 0; j < scores.length - 1 - i; j++) {if (scores[j] > scores[j + 1]) {temp = scores[j];scores[j] = scores[j + 1];scores[j + 1] = temp;}}}return scores[0];}//求均值public double getAvg() {int sum = 0;for (int score : this.scores) {sum += score;}return new BigDecimal(sum).divide(new BigDecimal(this.scores.length),2, BigDecimal.ROUND_HALF_UP).doubleValue();}//排序public void sort() {int temp;for (int i = 0; i < this.scores.length; i++) {for (int j = 0; j < this.scores.length - 1 - i; j++) {if (this.scores[j] > this.scores[j + 1]) {temp = this.scores[j];this.scores[j] = this.scores[j + 1];this.scores[j + 1] = temp;}}}}//静态说明类public static void explain() {System.out.println("本类[ScoreArray]实现了数组的:求最值[getMax()]、求均值[getAvg()]和排序[sort()]方法");}}public class StudentScoreArray extends ScoreArray {public StudentScoreArray(int[] scores) {super(scores);}//统计public void statistic() {super.sort();Map
5、另外需要遍历这个map实现统计结果的打印,我这里使用了java8以后才支持的Lambda表达式,所以你要运行这个程序必须要使用jdk1.8以上的版本。
6、如果你觉得这样不妥,可以网上再搜一下map的遍历方式。
7、运行结果:啊 代码又一坨的的挤在一起了,百度知道这个真是无语,我把几个关键的地方截个图给你康康:ScoreArray.java忘了说了,构造的同时还统计了有效分数(0~100)的数量冒泡排序后取最大值冒泡排序后取最小值利用java 的BigDecimal类解决除法运算精度的问题,四舍五入并保留了两位小数就是冒泡排序,从小到大StudentScoreArray.java:注意我说的map那里Test1.java:测试用例都使用的数组:int[] scores = {59, 60, 82, 58, 71, 99, 0, 59, 65};。
本文到此分享完毕,希望对大家有所帮助。
版权说明: 本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
猜你喜欢:
- 2022-09-24 在深秋歌词是什么意思(在深秋歌词)
- 2022-10-13 正定庙会是几月几号(正定庙会)
- 2022-08-30 GPI是指(gpi是什么意思)
- 2022-10-08 奔驰3系敞篷(3系敞篷)
- 2022-09-04 青盲第二部第22集(青盲第二部)
- 2022-05-09 塔里木发现石油 发现了多少石油什么情况
- 2022-08-19 OPPO A100 A115K(oppo a100)
- 2022-08-25 za化妆品是什么牌子(za化妆品价格表)
最新文章:
- 2022-11-22 百旺杏林湾效果图(百旺 杏林湾)
- 2022-11-22 哪里学习烧烤技术好(哪里学烧烤技术)
- 2022-11-22 那些曾为你流下的眼泪原唱(那些曾为你流下的眼泪)
- 2022-11-22 实用中医方药辞典(关于实用中医方药辞典介绍)
- 2022-11-22 《邪帝校园行》(邪帝校园行全文阅读)
- 2022-11-22 实用中医方药手册 第4版(关于实用中医方药手册 第4版介绍)
- 2022-11-22 实用中医方药丛书:中医秘方全书(关于实用中医方药丛书:中医秘方全书介绍)
- 热点推荐
- 热评文章