package com.qibu;
import java.util.Scanner;
import java.util.Arrays;
public class Qibujava {
public static void main(String[] args){
Qibujava hello=new Qibujava();
int[] nums=hello.ChengJi();
System.out.println("前3名的分数为:"+Arrays.toString(nums));
}
public int[] ChengJi( ){
int[] nums={89,-23,64,91,119,52,73};
Arrays.sort(nums);
int count=0;
for(int i=nums.length-1;i>=0;i--)
{
if(nums[i]<0||nums[i]>100){
continue;
}
count++;
if(count>3){
break;
}
}
return nums;
}
}
你的for循环没有改变数组的元素数值,你只需将public int[] ChengJi( )这个方法改为 int[] nums={89,-23,64,91,119,52,73}; Arrays.sort(nums); return nums; 就够了,在主方法输出结果让数组输出三个数后停止运行 System.out.println("前3名的分数为:"); for(int a2=0,a1=6;a1>=0;a1--){ //a2用来控制输出的数值个数 if(nums[a1]>=0&&nums[a1]<=100){ System.out.println(nums[a1]); a2++;} if(a2==3){ break;} }
public int[] ChengJi( ){
int[] nums={89,-23,64,91,119,52,73};
Arrays.sort(nums);
int[] num3 = new int[3];
int count=0;
for(int i=nums.length-1;i>=0;i--)
{
if(nums[i]>0&&nums[i]<100){
num3[count]=nums[i];
count++;
}
else continue;
if(count>2){
break;
}
}
return num3;
}
采纳呦呦呦