用帶參帶返回值不能完成這個(gè)嗎?
import java.util.Arrays;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? System.out.println("考試成績(jī)的前三名:");
? ? ? ? int scores[] = {89, -23, 64, 91, 119, 52, 73};
? ? ? ? HelloWorld hello = new HelloWorld(); ? ?
? ? ? ? int b = hello.count(scores);
? ? ? ? System.out.println(b);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
public int count(int scores[]){
? ? Arrays.sort(scores);
? ? int num = 0;
? ? for(int i = scores.length-1; i >= 0; i--){
? ? ? ? int a = 0;
? ? ? ? if(scores[i] > 100 || scores[i] < 0){
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? a = scores[i];
? ? ? ? num++;
? ? ? ? if(num > 3){
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return a;
? ? }
}
}
請(qǐng)問(wèn)這樣錯(cuò)在哪里呢
2017-06-14
用帶參帶返回值可以完成:
package test4;
import java.util.Arrays;
public class Test45 {
? ?//完成 main 方法
? ?public static void main(String[] args) {
? ? ? ?System.out.println("考試成績(jī)的前三名:");
? ? ? ?int scores[] = {89, -23, 64, 91, 119, 52, 73};
? ? ? ?Test45 hello = new Test45(); ? ?
? ? ? ?int[] b = hello.count(scores);//將返回值賦給數(shù)組b
? ? ? ?System.out.println(Arrays.toString(b));//用Arrays.toString方法輸出數(shù)組b的內(nèi)容
? ?}
? ?//定義方法完成成績(jī)排序并輸出前三名的功能
public int[] count(int[] scores){//返回值類型應(yīng)該為數(shù)組int[]
? ? ? ? int[] a = new int[3];//定義數(shù)組a的長(zhǎng)度
? ?Arrays.sort(scores);//排序
? ?int num = 0;
? ?for(int i = scores.length-1; i >= 0; i--){
? ? ? ?if(scores[i] > 100 || scores[i] < 0){
? ? ? ? ? ?continue;
? ? ? ?}
? ? ? ?if(num >= 3){
? ? ? ? ? ?break;
? ? ? ?}else{//數(shù)組a賦值前三名的成績(jī)
? ? ? ? a[num] = scores[i];
? ? ? ? }
? ? ? ? ? ? num++;
? ?}
? ? ? ? return a;//返回?cái)?shù)組a的內(nèi)容
}
}
改動(dòng)的地方都有注釋,可以看著理解一下
2017-06-11
你的數(shù)組聲明就有問(wèn)題 ?應(yīng)該是 int[] scores??= {89, -23, 64, 91, 119, 52, 73};吧 ?還有就是傳參 ?也應(yīng)該是public int count(int[] scores)