package com.lianxi;
import java.util.Arrays;
public class Biancheng {
public static void main(String[] args) {
Biancheng hello=new Biancheng();
int[] nums=hello.print(7);
Arrays.sort(nums);
System.out.println("考试成绩的前三名为:");
System.out.println(Arrays.toString(nums));
System.out.println(nums[6]);
System.out.println(nums[5]);
System.out.println(nums[4]);
}
public int[] print(int length) {
int[] scores={89,-23,64,91,119,52,73};
int[] nums=new int[length];
for(int i=0;i<scores.length;i++){
if(0<scores[i]&&scores[i]<100){
int j=0;
nums[j]=scores[i];
j++;
}
}
return nums;
}
}
哪里不对了?
你写的乱了,建议你研究一下这个
import java.util.Arrays;
public class Biancheng {
public static void main(String[] args) {
Biancheng hello=new Biancheng();
int[] nums=hello.print(7);
Arrays.sort(nums);
System.out.println("考试成绩的前三名为:");
// System.out.println(Arrays.toString(nums));
System.out.println(nums[6]);
System.out.println(nums[5]);
System.out.println(nums[4]);
}
public int[] print(int length) {
int[] scores={89,-23,64,91,119,52,73};
int[] nums=new int[length];
for(int i=0;i<scores.length;i++){
if(scores[i]<0||scores[i]>100){
continue;
}
nums[i]=scores[i];
} return nums;
}
}
调好了,不一定非得用给定的答案!
代码有些混乱,有些代码逻辑不够明确。写代码时先要缕清思路。给你个代码参考下
int scores[]=new int[]{89,-23,64,91,119,52,73}; int count=0;//统计输出个数 Arrays.sort(scores);//升序排序 for(int i=scores.length-1;i>=0;i--){//从成绩最大的开始循环 if(scores[i]<0||scores[i]>100) continue;//不符合范围的分数不能进入循环 System.out.println(scores[i]);//输出成绩 count++; if(count>2) break;//输出3个成绩后终止循环 }