輸出結(jié)果不對(duì),能過(guò)。這是。。。
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int []scores= {89 , -23 , 64 , 91 , 119 , 52 , 73};
? ? ? ? int []getscores=hello.getScores(scores);
? ? ? ? for (int i = 0; i < getscores.length; i++) {
System.out.println(getscores[i]);
}
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public int[] getScores(int [] scores){
? ? ? ? int count=0;
? ? ? ? int []No3=new int[3];
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ??
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? count++;
? ? ? ? ? ? if(count>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? No3[count--]=scores[i];
? ? ? ? ? ? }
? ? ? ? ??
? ? ? ? ? ?}
? ? ? ??
? ? ? ?return No3;
? ? }
}
2020-02-05
這樣寫(xiě)不就好了么,用ArrayList而不是List
2020-02-03
只說(shuō)你的結(jié)果為啥不多奧,其他的先不說(shuō),首先看到你的count++,系統(tǒng)在執(zhí)行完這條語(yǔ)句之后,count變?yōu)榱?,好,因?yàn)檫@里的count等于1,所以執(zhí)行else里的語(yǔ)句:count--,這里又把count降為了0,然后結(jié)束了本次循環(huán),下次循環(huán)再執(zhí)行到count++時(shí),由于count已經(jīng)被降為0了,所以count又等于1,然后進(jìn)入了一個(gè)假性的死循環(huán),一直執(zhí)行完了整個(gè)for循環(huán),所以最后52被賦值給了No3[1],為啥是下標(biāo)是1呢,因?yàn)閏ount--等于count還沒(méi)改變的值,就等于1。反正我是不知道改啥了,想過(guò)一個(gè)思路吧,最好不要count++,count--一起使用。
2020-02-03
直接sort,然后reverse,再用下標(biāo)輸出
2020-01-19
主函數(shù)里的for循環(huán)不需要,在你getscores里加個(gè)輸出語(yǔ)句就好了,
count為什么是count--,不需要用數(shù)組來(lái)保存得到的3個(gè)數(shù),直接輸出就好了。