求說出結(jié)果,順便解釋一下
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld main= new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? main.hello(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public static void hello(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int count=0;
? ? ? ? int[] nums=new int[3];
? ? ? ?for(int i=scores.length-1;i>=0;i--) {
? ? ? ? ? ?if(scores[i]<100&&scores[i]>0){
? ? ? ? ? ? ? nums[count]=scores[i];
? ? ? ? ? ? ? count++;
? ? ? ? ? ?}
? ? ? ? ? ?
? ? ? ?}
? ? ? ?for(int num:nums){
? ? ? ? ? ?System.out.print(num);
? ? ? ?}
? ? }
}
2016-07-19
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 a = new HelloWorld();
? ? ? ? a.sort(scores);?
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void sort(int[] scores)
? ? {
? ? ? ? int[] getScores = 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)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? getScores[j] = scores[i];
? ? ? ? ? ? j++;
? ? ? ? ? ? if(j > 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for(int i = 0; i < getScores.length; i++)
? ? ? ? {
? ? ? ? ? ? System.out.println("第" + (i + 1) + "名是:" + getScores[i] + "分。");
? ? ? ? }
? ? } ? ?
}
/************結(jié)果是**************
第1名是:91分。
第2名是:89分。
第3名是:73分。
**********************************/
2016-07-19
91 ? 89 ? 73