請問我這個報錯[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};
? ? System.out.println("考試成績的前三名為:");
? ? HelloWorld hello = new HelloWorld();
? ? hello.cj(scores);
? ? int[] a= hello.cj(scores);
? ? System.out.println(a);
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] cj(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>2){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }return scores;
? ? }
? ?
}
2018-11-18
a是int[],你直接輸出,當(dāng)然是打印地址,
2018-11-22
因為你想輸出的是數(shù)組,所以打印成這樣.數(shù)組輸出需要轉(zhuǎn)換一下:Arrays.toString(a),你改一下就好了。?System.out.println(Arrays.toString(a));
2018-11-21
錯誤1:num++;? if(num>2){ break;}? 假如在上一輪num=2,意味著已經(jīng)有兩個有效成績。在這一輪中,假如有效,你首先就加了個1,就變成3,進入if,直接break退出了,根本不會輸出這第三個有效成績。正確的順序應(yīng)該是先加一,再輸出,再判斷。
錯誤2:return scores; int[] a= hello.cj(scores); System.out.println(a); 這個我也說不太清楚,但是我覺得應(yīng)該是不能這樣子的吧,不說語法,從邏輯來看,scores只是升序排序之后的數(shù)組,你再輸出來和題目不符,題目是前三高。我覺得cj方法可以不用返回直接用void,在cj方法里面輸出,當(dāng)你判斷數(shù)組里這個數(shù)滿足要求你就可以輸出它,增一條語句比你這個要稍微簡單點:System.out.prinln(scores[i]);? main方法里呢只要調(diào)用cj方法就好,hello.cj(scores);
2018-11-18
你的代碼里
int[] a= hello.cj(scores);
? ? System.out.println(a);
這里沒必要,不需要寫,直接刪了
if(num>2)這里只會輸出倆個數(shù)字,應(yīng)該改成if(num>3)
2018-11-18
import java.util.Arrays;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? int[] scores ={89,-23,64,91,119,52,73};
? ? System.out.println("考試成績的前三名為:");
? ? HelloWorld hello = new HelloWorld();
? ? hello.cj(scores);
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] cj(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;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }
return scores;
? ? }
}
? ??
? ??
? ??
? ??
? ??
? ??
試試這樣
2018-11-18
不需要返回值