找不到哪錯了,求大神指教!
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的話,會報數組越界的吧
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越界了,應該改成i=scores.length-1
2016-08-08
1.for(int?i=scores.length;i>=0;i--){ ????scores.length應該改為scores.length-1;此數組中沒有第7位元素,只有0,1,2,3,4,5,6; 2.int?num=0; ????num的定義要移到for循環外; 3.??if(num<=3){ ???????System.out.println(scores[i]); ????} ????1)應該是num>3。這是一個判斷,取三個成績就跳出循環,不再執行,因此 ????2)if語句的語句塊應該是break,跳出循環 ????3)System.out.println(scores[i]);這個輸出語句要移到if語句外,但要在for循環中 正確代碼: package?day02; import?java.util.Arrays; public?class?Test06?{ public?static?void?main(String[]?args)?{ int[]?scores?=?{89?,?-23?,?64?,?91?,?119?,?52?,?73}; Test06?t?=?new?Test06(); t.showTop3(scores); } public?void?showTop3(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]); } } }2016-08-08
...大胸弟,if(num<=3)...這不是循環語句。,
一般不確定循環次數的用while
確定循環次數的用for ,switch