找不到哪錯了,求大神指教!
import java.util.Arrays;
public class HelloWorld {
?? ?public static void main(String[] args) {
?? ??? ???????? int scores[]=new int[]{89,-23,64,91,119,52,73};
?? ??? ??? ??? ?HelloWorld Hello=new HelloWorld();
?? ??? ??? ??? ?System.out.println("前三名成績是:");
?? ??? ???????? Hello.rangKing(scores);
?? ??? ???? ?
?? ??? ???? }
?? ??? ??? ?
?? ??? ???? //定義方法完成成績排序并輸出前三名的功能
?? ??? ???? public void rangKing(int scores[]){
?? ??? ??? ??? ?Arrays.sort(scores);
?? ??? ???????? for(int i=scores.length;i>=0;i--){
?? ??? ??????? ??? ?int num=0;
?? ??? ???????????? if(scores[i]<0||scores[i]>100)
?? ??? ??????????? ??? ?continue;
?? ??? ???????????? num++;
?? ??? ???????????? if(num<=3){
?? ??? ???????????? System.out.println(scores[i]);
?? ??? ???????????? }
?? ??? ???????? }
?? ??? ??????? ?
?? ??? ???????? }
?? ??? ???? }
???
2016-08-08
i=scores.length-1,如果是=scores.length的話,會報數(shù)組越界的吧
2016-08-08
? ? public void rangKing(int scores[]){
? ? ? ? ? ? ? ? Arrays.sort(scores);
? ? ? ? ? ? ? ? ?int num=0;
? ? ? ? ? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? if(scores[i]>0&&scores[i]<100)
? ? ? ? ? ? ? ? ? ? {num++;
? ? ? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(num==3)break;
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
2016-08-08
? ?for(int i=scores.length;i>=0;i--)
這句錯了,i=scores.length越界了,應(yīng)該改成i=scores.length-1
2016-08-08
2016-08-08
...大胸弟,if(num<=3)...這不是循環(huán)語句。,
一般不確定循環(huán)次數(shù)的用while
確定循環(huán)次數(shù)的用for ,switch