哪位大神幫我看看為什么我這個程序運行無結(jié)果,謝謝?
package com.imooc;
import java.util.Arrays;
public class HelloWorld {
public int[] getScores(int[] scores){
Arrays.sort(scores);
int j = 0;
int[] newScores = new int[j];
for(int i = 0;i < scores.length;i++){
if(scores[i] < 0&&scores[i] > 100){
continue;
}
else{
newScores[j] = scores[i];
j++;
}
}
return newScores;
}
public static void main(String[] args){
HelloWorld hello = new HelloWorld();
int[] scores = {89,-23,64,91,119,52,73};
int[] newScores = hello.getScores(scores);
System.out.println(Arrays.toString(newScores));
}
}
2016-01-04
????
不客氣。你可以試一試這樣。
2016-01-04
你這個程序好多語法錯誤:
第4行,定義方法怎么用int[]?
第7行,初始化成績數(shù)組怎么是new int[j](⊙o⊙)…
第9行,條件是或,不是且
第17行,返回值是多余的
第23行,根本不用toString的方法
給你一個改進建議,不要用一個新數(shù)組了,題目沒有要求,你直接改變原數(shù)組就行了
2016-01-04
你這里將newScores數(shù)組的大小設(shè)置為0了。所以運行到newScores[j] = scores[i];這里會報錯?java.lang.ArrayIndexOutOfBoundsException(數(shù)組下標越界異常)。