定义数组来筛选成绩
7个栗 2019-08-04 06:07:53 1、 定义一个成绩数组 scores
2、保存预定的考试成绩 89 , -23 , 64 , 91 , 119 , 52 , 73
思路:
: 1、 在方法中使用 Arrays 类的 sort( ) 方法对数组进行排序,默认按升序排列,注意 Arrays 类的使用需要导入 java.util.Arrays
2、 由于只需要输出考试成绩的前三名,所以定义一个变量,统计有效成绩的前三名的数量
3、 使用 for 循环遍历数组中的元素,由于要输出的是前三名成绩,所以从后往前遍历,即倒序遍历 如 for ( int i = scores.length - 1; i >= 0; i-- ) { }
4、 判断成绩的有效性,如果成绩小于 0 或大于 100 ,则使用 continue 忽略此成绩
5、 如果成绩有效,则有效成绩数加 1 。判断有效成绩的个数,如
代码:
import java.util.Arrays
public static void main(String[args])(
int [ ] scores =[ 89 , -23 , 64 , 91 , 119 , 52 , 73 ];
int count = 0;
for(int i =0 ; i < scores.length–1; i++){
if(scores < 0 || scores>100){
count++;
continue{;
} Arrays.sort(scores);
for(int j = scores.length – 1; j => scores.length-3; j––){
System.out.print(j+" ") ;
System.out.print(7–count);
}
}}
第一次)拿手机敲代码,写的不好。大家不要太介意,我会慢慢改正的。