求大神解答;運(yùn)行結(jié)果為:考試成績(jī)的前三名為[I@15db9742,運(yùn)行結(jié)果應(yīng)該是前三名的成績(jī),怎么是亂碼,哪里錯(cuò)了
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("考試成績(jī)的前三名為"+count);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? 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("考試成績(jī)的前三名為"+count);
這里count是一個(gè)數(shù)組,不能直接輸出,需要用Arrays.toString()類輸出
2.return scores;
你返回的scores,其實(shí)在方法show里,你并沒有改變scores數(shù)組里的任何值,返回的還是原來的scores,里面的元素并沒有改變。
3.你寫的代碼還有太多需要改進(jìn)的地方,太過冗余,多參考慕課給的正確答案。
2018-05-20
輸出的是數(shù)組的指針,感覺題主的需求是輸出整個(gè)數(shù)組吧。可以用Arrays.deepToString,Arrays.deepToString與Arrays.toString不同之處在于,Arrays.deepToString更適合打印多維數(shù)組
2018-05-19
額,那個(gè)不是亂碼,是內(nèi)存的地址值,你要用數(shù)組輸出前三名的話,應(yīng)該加個(gè)for循環(huán)給count增加標(biāo)簽,不過我還是不知道你的需求是什么
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 【】 ?把【】去掉;