為什么這樣會錯?
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores = {89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ? HelloWorld hello = new HelloWorld();
? ? ? ? System.out.println("考試的前三名為:"+hello.threescore(scores));
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void threescore(int[] scores) {
? ? ? ? Arrays.sort(scores);
? ? ? ? int nums = 0 ;
? ? ? ? for(int i = scores.length-1;i>=0;i--) {
? ? ? ? ? ? if(scores[i]<0 || scores[i]>100 ) {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? nums++;
? ? ? ? ? ? if(nums>3) {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? System.out.println(scores[i]);
? ? ? ? }
}
2016-01-25
改成if(num > 2){break;} 不然就會輸出四個數(shù)了。
2016-01-23
System.out.println("考試的前三名為:"+hello.threescore(scores));
不能寫在System.out.println里它是void的方法返回值是void
要單獨寫
System.out.println("考試的前三名為:");
hello.threescore(scores);
2016-01-23
System.out.println("考試的前三名為:"+hello.threescore(scores));
是這一句有問題吧?
這里面threescore返回值為空,void
不應(yīng)該再用來輸出了