求幫忙看看哪里錯了
import java.util.Arrays;
public class HelloWorld {
? ? public static void main(String[] args) {
? ? ? ? int [] scores={89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試成績的前三名為");
?? ? ? HelloWorld hello=new HelloWorld();
? ? ? hello.scoreMaxs(scores);
?? ? ? ?
? ? }
? ? ? ? public void scoreMaxs(int [] scores){
? ? ? ? ? ? Arrays.sort(scores);
? ? ? ? int j=0;
? ? ? ? for(int i=scores.length;i>=0;i--){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? continue;
? ? ? ? ? }
? ? ? ? ? j++;
? ? ? ? ? System.out.println(scores[i]);
?? ? ? ? if(j>3){
?? ? ? ? ? ? break;
?? ? ? ? }?
? ? ? ? }
? ? ? ?
?? ? }
}
2019-03-26
樓上的正解.其實循環(huán)判斷那里不一定要>=,>也可以的
把輸出System.out.println(scores[i]);放出循環(huán)就可以了
2019-03-24
public void scoreMaxs(int [] scores){
Arrays.sort(scores);
int j=0;
for(int i=scores.length-1;i>=0;i--){
if(scores[i]<0||scores[i]>100){
continue;
}
j++;
System.out.println(scores[i]);
if(j>=3)
break;
}
按照你的思路修改后的。
int i的初始值應(yīng)該是 scores.length-1。數(shù)組長度是7,但最后一個元素是scores[6]就是最后一個了。
(這里改了就能運行了)
"j>3" 改為 "j>=3"不然會輸出4個元素。
break 前后的"{ }"可以省略。
***初學(xué)者,第一次指點別人,想了好久***
2019-03-24
最后多了一個花括號 刪掉