入門第一季的最后編程練習麻煩看下錯在哪兒了
import java.util.*;
public class Test1{
??? public static void main(String[] args) {
?? ??? ?int [] scores={89,-23,64,91,119,52,73};
??????? int [] list;
??????? Test1 hello=new Test1();
??????? list=hello.show(scores);
??????? System.out.println(Arrays.toString(list));
??? }
?? ?
??? //定義方法完成成績排序并輸出前三名的功能
??? public int[] show(int [] scores){
??????? int sum[]=new int[3];
??????? int count=0;
??????? int j=0;
??????? Arrays.sort(scores);
??????? for(int i=scores.length-1;i>=0;i--){
??????????? if(scores[i]>=0&&scores[i]<=100){?????????? ?
??????????????? sum[j]=scores[i];
??????????????? count++;
??????????????? j++;
??????????? }
??????????? if(count>3)
??????????????? break;
??????? }
??????? return sum;
?? ?}
?? ?
}
語法沒問題。就是運行的時候出錯了。有誰可以幫忙看看么。謝謝
2016-11-01
我把你的代碼稍微做了點修改后,運行過是沒問題的。但是輸出成績的要求是一行一行的,所以我覺得輸出還是放在循環里面比較好。
import java.util.*;
public class Test1{
? ? public static void main(String[] args) {
? ? ? ? int [] scores={89,-23,64,91,119,52,73};
? ? ? ? int [] list=new int[3];
? ? ? ? System.out.println("考試成績前三名為:");
? ? ? ? Test1 hello=new Test1();
? ? ? ? list=hello.show(scores);
? ? ? ? System.out.println(Arrays.toString(list));
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] show(int [] scores){
? ? ? ? int sum[]=new int[3];
? ? ? ?
? ? ? ? int j=0;
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]>=0&&scores[i]<=100){ ? ? ? ? ? ?
? ? ? ? ? ? ? ? sum[j]=scores[i];
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? ? ? if(j>=3)
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return sum;
? ? }
? ??
}
2016-11-01
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int [] numbers = {89, -23,64,91,119,52,73};
? ? ? ? HelloWorld hello = new HelloWorld();
? ? ? ? hello.grade(numbers);
? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void grade(int [] numbers){
? ? ? ? java.util.Arrays.sort(numbers);
? ? ? ? int n = 0;
? ? ? ? for(int i = numbers.length-1;i >= 0 ;i--){
? ? ? ? ? ? if(n>=3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? if(numbers[i] > 100||numbers[i]<0){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? System.out.println(numbers[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? n++;
? ? ? ? ? ? }
? ? ? ? }
? ? }?
}
2016-11-01
導入的包沒有導入正確。少了Array