慕粉2239006113
2017-03-01 21:17


打印的是数组的地址,而没有指定数组元素
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
hello.list(scores);
}
public void list(int []scores) {
Arrays.sort(scores);
int count = 0;
int []topThree = new int [3];
for(int i = scores.length-1;i>=0;i--){
if (scores[i] < 0 || scores[i] >100)
continue;
topThree[count] = scores[i];
count++;
if(count == 3)
break;
}
System.out.println(Arrays.toString(topThree));
}
}
试试这个咯
import java.util.Arrays;
public class HelloWorld {
//完成 main 方法
public static void main(String[] args) {
int[] scores={89,-23,64,91,119,52,73};
HelloWorld hello=new HelloWorld();
System.out.println(Arrays.toString(getArray(scores)));
}
//定义方法完成成绩排序并输出前三名的功能
public static int[] getArray(int[] array)
{
Arrays.sort(array);
int count=-1;
int[] newArray=new int[3];
for(int i=array.length-1;i>=0;i--)
{
if(array[i]<0||array[i]>100)
{
continue;
}
count++;
newArray[count]=array[i];
if(count==2)
{
break;
}
}
return newArray;
}
}我不知道写的好不好,以后再回来改吧
把 count定义到循环外,不然每次循环count都重新置0
你没输出前三名啊
Java入门第一季(IDEA工具)
1168080 学习 · 18753 问题
相似问题