码迷,mamicode.com
首页 > 编程语言 > 详细

imooc-java-作业练习-20150918

时间:2015-09-18 11:47:47      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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

要求:

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

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

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

运行效果:

技术分享

 1 public class HelloWorld {
 2     
 3       // 完成 main 方法
 4     public static void main(String[] args) {
 5         int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
 6         scores = sortCustom(scores);
 7         for (int n = 0; n < 3; n++) {
 8             System.out.println(scores[n]);
 9         }
10     }
11 
12     // 定义方法完成成绩排序并输出前三名的功能
13     public static int[] sortCustom(int[] scores) {
14         int tmp = 0;
15         for (int k = 0; k < scores.length - 1; k++) {
16             if (scores[k] > 100 || scores[k] < 0) {
17                 for (int m = scores.length - 1; m > k; m--) {
18                     if (scores[m] <= 100 && scores[m] >= 0) {
19                         tmp = scores[m];
20                         scores[m] = scores[k];
21                         scores[k] = tmp;
22                         break;
23                     }
24                 }
25             }
26         }// 将不规范的成绩置于后方
27         for (int i = 0; i < scores.length; i++) {
28             // 这里是自定义的排序,没有使用array的排序
29             for (int j = i + 1; j < scores.length; j++) {
30                 //忽略不规范的成绩
31                 if (scores[j] > 100 || scores[j] < 0) {
32                     continue;
33                 }
34                 //交换数据
35                 if (scores[j] > scores[i]) {
36                     tmp = scores[i];
37                     scores[i] = scores[j];
38                     scores[j] = tmp;
39                 }
40             }
41         }
42         return scores;
43     }
44 }


运行结果正确无误。

 

imooc-java-作业练习-20150918

标签:

原文地址:http://www.cnblogs.com/xxkalychen/p/4818670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!