我個(gè)人覺得這題對我來講還是有一定難度,如果不借用排序,我只能做到排出前兩名,有能夠排出前三名的大佬歡迎留言!
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? int[] scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ??
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? System.out.println("成績前三名為:");
? ? ? ? hello.showThree(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void showThree(int[] scores){
? ? ? ? for(int i=0;i<1;i++){
? ? ? ? ? ? int max=scores[0],max2=scores[1],max3=scores[2];
? ? ? ? ? ? for(int j=0;j<scores.length;j++){
? ? ? ? ? ? ? ? if(max<scores[j]){
? ? ? ? ? ? ? ? ? ? max2=max;
? ? ? ? ? ? ? ? ? ? max=scores[j];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(max);
? ? ? ? ? ? ?System.out.println(max2);
? ? ? ? ? ? ?System.out.println(max3);
? ? ? ? }
? ? }
? ??
}
2019-09-05
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試前三名為:");
? ? ? ? hello.showTop3(scores);
? ? ? ??
? ? }? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void showTop3(int[] scores){
? ? ? ? int temp,count=0;
? ? ? ? for(int i=0;i<scores.length-1;i++){? //類似冒泡排序
? ? ? ? for(int j=i+1;j<scores.length;j++){
? ? ? ? if(scores[i]<scores[j]){
? ? ? ? temp=scores[i];
? ? ? ? scores[i]=scores[j];
? ? ? ? scores[j]=temp;
? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? ? ? for(int i=0;i<scores.length;i++){
? ? ? ? if(scores[i]<0 || scores[i]>100)
? ? ? ? {
? ? ? ? continue;
? ? ? ? }
? ? ? ? count++;
? ? ? ? if(count<=3){
? ? ? ? System.out.println(scores[i]);
? ? ? ? }
? ? ? ?
? ? ? ? }
? ? }
??
}
2019-09-06
2019-09-06
http://idcbgp.cn/qadetail/330593,昨晚自己利用冒泡排序重新思考了一下,大家可以看一下。