寫了個返回數(shù)組的方法實現(xiàn)了,調(diào)試沒問題,大佬們請幫看看有沒有需要改進(jìn)的地方
import java.util.Arrays;
public class HelloWorld {
public int[] showTop3(int[] score) {
Arrays.sort(score);
? ? ? ? int i=0,j;
? ? ? ? int[] Top3=new int[3];
? ? ? ? ? ? for(j=score.length-1; j>0; j--) {
? ? ? ? ? ? if (score[j]>=0 && score[j]<=100){
? ? ? ? ? ? ? ? ? ? Top3[i] = score[j];
? ? ? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? ? ? if(i>=3)
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ??
? ? ?}
? ? ? ? return Top3;
? ?}
public static void main(String[] args) {
int[] scores = {89, -23, 64, 91, 119, 52, 73};
HelloWorld test = new HelloWorld();
int[] top3 = test.showTop3(scores);
System.out.println("考試成績的前三名為:");
for(int score : top3) {
System.out.println(score);
}
}
}
2019-02-24
感覺看上去有點違背習(xí)慣,建議將public static void main(String[] args) 這段放前邊,后邊的類在后邊看上去舒服些
2019-03-13
沒做數(shù)據(jù)合法檢測,附上
import java.util.Arrays;
public class HelloWorld {
?? ?
??? //完成 main 方法
??? public static void main(String[] args) {
?????? ?
??????? int [] scores = {89,-23,64,91,119,52,73};
??????? HelloWorld test = new HelloWorld();
??????? test.Output(scores);
?????? ?
?????? ?
??? }
?? ?
??? //定義方法完成成績排序并輸出前三名的功能
??? public void Output(int [] array){
??????? Arrays.sort(array);
??????? int i = 0,j = array.length-1,num = 0;
??????? while (array[i] < 0)
??????? {
??????????? i++;
??????? }
??????? while (array[j] > 100)
??????? {
??????????? j--;
??????? }
??????? System.out.println("考試成績的前三名為:" + "\n");
??????? while (j >= i && num < 3)
??????? {
??????????? System.out.println(array[j] + "\n");
??????????? j--;
??????????? num++;
??????? }
??? }
???
2019-03-01
確定調(diào)試會沒有問題嗎?你定義的數(shù)組名是? scores,用的時候卻變成了score.....
2019-02-22
import java.util.Arrays;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores = {89,-23,64,91,119,52,73};
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void shu(int[] scores) {
? ? ? ? Arrays.sort(scores);
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? for(int i = scores.length-1;i>scores.length-4;i--) {
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }
? ? }
}
/*你覺得這段代碼如何?*/
2019-02-21
沒毛病? 挺好的