新人提問:
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello = new HelloWorld();
? ? ? ? int[] scores = {89,-23,64,91,119,52,73};
? ? ? ? hello.abc(scores);
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? System.out.println(hello.abc(scores));
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void abc(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int count = 0;
? ? ? ? for(int i = scores.length -1;count <3&&i>=0;i++){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100)
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? count++;
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }
? ? }
}
我愣是沒找到錯在哪里?老是提示void類型不允許
2022-03-24
嘿嘿不用著急,改正錯誤代碼才是學習的好機會。第12行代碼換成document.write("<br>");就可以了
2018-05-18
abc方法void表示沒有返回值或者空返回值,然后你還讓他打印,所以或拋異常??梢园蒳++改成i--,然后調用abc()方法之后,直接遍歷scores數組。不用?System.out.println(hello.abc(scores));這一句
2018-05-05
?System.out.println(hello.abc(scores));這個完全是多余的。還有i++改成i--