Java基础练习题:编程练习(1)

2017年8月14日18:51:29 发表评论 2,841 views

要求:

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

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

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

 

import java.util.*;
public class HelloWorld {
    
    //完成 main 方法
    public static void main(String[] args) {
    	
		int[] scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
		HelloWorld a=new HelloWorld();
		a.ranking(scores);
    }
    
    //定义方法完成成绩排序并输出前三名的功能
    public void ranking(int[] scores){
		for(int i=0;i<scores.length;i++){
			if(scores[i]>100||scores[i]<0){
				scores[i]=0;
			}
		}
		Arrays.sort(scores);
		for(int i=1;i<4;i++){
			System.out.println("第"+i+"名的分数是:"+scores[scores.length-i]);
		}
    }
}

 

这我是那个经典50题,我学习网站出的练习编程练习。其实前面有练习了好几个,不过,这个比较综合点。今天就记录这个。

 

 

  • A+
所属分类:JAVA

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: