參考大家的方法得到結(jié)果
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? int[] scores = new int[]{89,-23,64,91,119,52,73};
? ? ? HelloWorld hello = new HelloWorld();
? ? ? hello.sort(scores);
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void sort(int[] scores)
? ? {
? ? ? ? /*int j = 0; ??
? ? ? ? int[] realscores = new int[10];
? ? ? ? for(int i = 0;i<scores.length;i++)
? ? ? ? { ??
? ? ? ? ? ??
? ? ? ? ? ? if(scores[i]>=0&&scores[i]<=100)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? realscores[j] = scores[i];
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Arrays.sort(realscores);
? ? ? ? System.out.println(realscores[9]);
? ? ? ? System.out.println(realscores[8]);
? ? ? ? System.out.println(realscores[7]);
? ? ? ? */
? ? ? ? ? Arrays.sort(scores);
? ? ? ? ? int j = 0;
? ? ? ? ? System.out.print("前三名成績是:");
? ? ? ? ? for(int i = scores.length-1;i>=0;i--)
? ? ? ? ? {
? ? ? ? ? ? ? if(scores[i]>=0&&scores[i]<=100)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.println(scores[i]); ?
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? ? ? if(j>=3)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? }
}
2015-12-18
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int scores[]={89,-23,64,91,119,52,73};//成績數(shù)組
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? HelloWorld getArray=new HelloWorld();//創(chuàng)建對象
? ? ? ? getArray.gettop3(scores);//調(diào)用方法
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void gettop3(int[] scores){
? ? ? ? Arrays.sort(scores);//排序
? ? ? ? int count=0;//計算數(shù)量
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<0 || scores[i]>100)//成績要求
? ? ? ? ? ? ? ? continue;
????????????count++;
? ? ? ? ? ? if(count>3)//當(dāng)循環(huán)到前三名時停止終止循環(huán)
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? System.out.println(scores[i]);//輸出前三名
? ? ? ? }
? ? }
? ??