運(yùn)行成功,但結(jié)果排序和答案不一樣,怎么解決?大神幫
//導(dǎo)入Arrays類
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};? ?
? ? hello.top3(scores);? ??
? ? System.out.println("考試成績(jī)前三名:");
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void top3(int[] scores){
? ? //定義一個(gè)變量,統(tǒng)計(jì)有效成績(jī)的前三名的數(shù)量
? ? int so=0;
? ??
? ? for ( int i = scores.length - 1; i >= 0; i-- ){
? ? ? ? if(scores[i]>0&&scores[i]>100){
? ? ? ? ? ??
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? so++;
? ? ? ? Arrays.sort(scores);
? ? ? ? System.out.println(scores[i]);
? ? }
? ?
? }
? ??
}
2020-05-01
將數(shù)組作為參數(shù)進(jìn)入函數(shù)后,開始并未對(duì)數(shù)組進(jìn)行處理,for語句取數(shù)組的最后一個(gè)元素為73,if語句不滿足,不執(zhí)行continue,循環(huán)繼續(xù),so++,數(shù)組排序,輸出排序后最后一個(gè)值,即最大值119,進(jìn)入第二次循環(huán),輸出倒數(shù)第二大的值91,后續(xù)一直未滿足if語句,持續(xù)輸出,直到遍歷完整個(gè)數(shù)組。
2020-04-30
? Arrays.sort(scores);排序應(yīng)該放在for循環(huán)前面,而且你的判斷語句也錯(cuò)了。
2020-04-23
你的判斷語句出錯(cuò)了,應(yīng)該是scores[i]<=100
2020-04-05
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();
? ? ? ? hello.sort(scores);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public int[] sort(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int count = 0;
? ? ? ? for(int i =scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<=100&&scores[i]>=0){
? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ??
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(count==3){
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return scores;