求大神解答;運行結果為:考試成績的前三名為[I@15db9742,運行結果應該是前三名的成績,怎么是亂碼,哪里錯了
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(); ? ?
? ? int [] count= hello.show(scores);
? ? System.out.println("考試成績的前三名為"+count);
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int [] show(int [] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num = 0;
? ? ? ? for(int i = scores.length-1;i >= 0;i--){
? ? ? ? ?if(scores[i] < 0 ||scores[i] > 100){
? ? ? ? ? ? ?continue;
? ? ? ? ?}
? ? ? ? ?num++;
? ? ? ? ?if(num > 3){
? ? ? ? ? ? ?break;
? ? ? ? ?}
? ? ? ? }
? ? ? ? return scores;
? ? }
? ??
? ??
? ??
? ??
? ??
? ??
? ??
? ??
? ??
}
2018-05-19
1.System.out.println("考試成績的前三名為"+count);
這里count是一個數組,不能直接輸出,需要用Arrays.toString()類輸出
2.return scores;
你返回的scores,其實在方法show里,你并沒有改變scores數組里的任何值,返回的還是原來的scores,里面的元素并沒有改變。
3.你寫的代碼還有太多需要改進的地方,太過冗余,多參考慕課給的正確答案。
2018-05-20
輸出的是數組的指針,感覺題主的需求是輸出整個數組吧??梢杂肁rrays.deepToString,Arrays.deepToString與Arrays.toString不同之處在于,Arrays.deepToString更適合打印多維數組
2018-05-19
額,那個不是亂碼,是內存的地址值,你要用數組輸出前三名的話,應該加個for循環給count增加標簽,不過我還是不知道你的需求是什么
2018-05-19
import java.util.Arrays;
pubilc class HelloWorld{
public static void main(String[] args){
int[] scores={89,-23,64,91,119,52,73};
HelloWorld hello=new HelloWorld();
hello.getScore(scores);
}
public void getScore(int[] scores){
Arrays.sort(scores);
int count=0;
for(int i=scores.length-1;i>=0;i--){
if(scores[i]<100&&scores[i]>0)
{
System.out.println(scores[i]);
count++;
}
if(count>=3)
break;
}
}
}
2018-05-19
定義方法那里?
public int 【】 ?把【】去掉;