小伙伴们,请根据所学知识,编写一个 JAVA 程序,实现输出考试成绩的前三名

要求:

1、 考试成绩已保存在数组 scores 中,数组元素依次为 89 , -23 , 64 , 91 , 119 , 52 , 73

2、 要求通过自定义方法来实现成绩排名并输出操作,将成绩数组作为参数传入

3、 要求判断成绩的有效性( 0—100 ),如果成绩无效,则忽略此成绩

运行效果:

 public class HelloWorld {

       // 完成 main 方法
public static void main(String[] args) {
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
scores = sortCustom(scores);
for (int n = 0; n < 3; n++) {
System.out.println(scores[n]);
}
} // 定义方法完成成绩排序并输出前三名的功能
public static int[] sortCustom(int[] scores) {
int tmp = 0;
for (int k = 0; k < scores.length - 1; k++) {
if (scores[k] > 100 || scores[k] < 0) {
for (int m = scores.length - 1; m > k; m--) {
if (scores[m] <= 100 && scores[m] >= 0) {
tmp = scores[m];
scores[m] = scores[k];
scores[k] = tmp;
break;
}
}
}
}// 将不规范的成绩置于后方
for (int i = 0; i < scores.length; i++) {
// 这里是自定义的排序,没有使用array的排序
for (int j = i + 1; j < scores.length; j++) {
//忽略不规范的成绩
if (scores[j] > 100 || scores[j] < 0) {
continue;
}
//交换数据
if (scores[j] > scores[i]) {
tmp = scores[i];
scores[i] = scores[j];
scores[j] = tmp;
}
}
}
return scores;
}
}

运行结果正确无误。

最新文章

  1. 【原】FMDB源码阅读(三)
  2. ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码
  3. 解决虚拟机 MAC10.9 不能设置1366*768分辨率问题
  4. 转:LIRE的使用
  5. Html-Css-iframe的自适应高度方案
  6. 使用node.js的bodyParser中间件读取post数据解析
  7. centos6.6下编译安装mysql5.6之后启动失败:Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
  8. 通过dblink impdp导入
  9. iOS动画技术笔记
  10. QF——iOS中数据持久化的几种方式
  11. Java字符串操作
  12. golang自定义路由控制实现(二)-流式注册接口以及支持RESTFUL
  13. 省市区地址三级联动jQuery插件Distpicker使用
  14. 3D Slicer Reconstruct CT/MRI
  15. node之jade和ejs的使用方法 jade篇
  16. [整理]CSS3 滤镜
  17. Scrapy计划表
  18. RESTful转载,多看几遍就理解了写点自己的看法和理解
  19. ContentProvider用法
  20. 自测之Lesson4:gdb

热门文章

  1. mysql 操作杂记
  2. 第3.3 案例2: 工作队列 job queue
  3. logistc regression练习(三)
  4. 浅谈大数据神器Spark中的RDD
  5. android assets文件夹浅谈
  6. 北大poj-2688
  7. “-webkit-appearance: none”按钮样式作用!
  8. centos 7 lamp (linux+apache+mysql+php)开发环境搭建(转+原创)
  9. JSONObject
  10. GitHub菜鸟日志1——20160531