代碼通過(guò)了,但是運(yùn)行結(jié)果不對(duì),求大神指點(diǎn)迷津
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.scoresSort(scores,scores.length);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void scoresSort(int[] scores,int num){
? ? ? ? int[] scoresCorrect=new int[num];
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1,j=0;i>=0;j++){
? ? ? ? ? ? if(scores[i]<0 && scores[i]>100){
? ? ? ? ? ? ? ? i--;
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? scoresCorrect[j]=scores[i];
? ? ? ? ? ? ? ? i--;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? for(int i=scoresCorrect.length-1;i>=scoresCorrect.length-3;i--){
? ? ? ? ? ? System.out.println(scoresCorrect[i]);
? ? ? ? }
? ? }
? ??
?這是我的錯(cuò)誤的運(yùn)行結(jié)果
這是正確的結(jié)果
2019-02-15
別說(shuō),你這bug挺多的。
1)score<0||score>100,是或不是且
2)for循環(huán)里第三項(xiàng)j++刪掉,把j++加進(jìn)else里;或者把j++改成i--,把句里的if和else里的i--全刪掉,再把j++加進(jìn)else
3)你是先把原數(shù)組從小到大排序之后,再?gòu)奈驳筋^有選擇的放進(jìn)correct數(shù)組里,所以correct數(shù)組本就已經(jīng)是從大到小的順序,你輸出的時(shí)候應(yīng)該從前向后輸出,不是從后向前輸出。
發(fā)一個(gè)給你debug之后的版本:
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.scoresSort(scores,scores.length);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void scoresSort(int[] scores,int num){
? ? ? ? int[] scoresCorrect=new int[num];
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1,j=0;i>=0;){
? ? ? ? ? ? if(scores[i]<0 || scores[i]>100){
? ? ? ? ? ? ? ? i--;
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? scoresCorrect[j]=scores[i];
? ? ? ? ? ? ? ? i--;
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? for(int i=0;i<3;i++){
? ? ? ? ? ? System.out.println(scoresCorrect[i]);
? ? ? ? }
? ? }
}
另外你寫這個(gè)好麻煩啊,直接輸出就行沒必要再列到數(shù)組輸出;數(shù)組長(zhǎng)度本就可以.length得到?jīng)]必要再設(shè)一個(gè)參數(shù);此外排序之后后面的數(shù)只可能大于100不可能小于零,不用判斷是否小于零;最后,你只需要前三個(gè),沒必要把所有有效數(shù)字都排一遍。
我發(fā)一下我寫的吧。
public void prints(int[] score){
? ? Arrays.sort(score);
? ? System.out.println("考試成績(jī)的前三名為:");
? ? int i=score.length-1;
? ? for(int j=1;j<=3;i--){
? ? if(score[i]>100)
? ? continue;
? ? else
? ? ? ? {System.out.println(score[i]);j++;}
? ? }
2019-02-14
?public static void main(String[] args) {
???????? int[] scores={89,-23,64,91,119,52,73};
???????? HelloWorld hello=new HelloWorld();
???????? hello.scoresSort(scores,scores.length-2);
???? }
????
???? //定義方法完成成績(jī)排序并輸出前三名的功能
???? public void scoresSort(int[] scores,int num){
???? ? int[] scoresCorrect=new int[num];
???????? Arrays.sort(scores);
???????? int i=0,j=0;
?????? for(i=0;i<scores.length;i++){
???????????? if(scores[i]>0 && scores[i]<100){
???????????? ?scoresCorrect[j] = scores[i];
???????????? ?j++;?
???????????? }
??????? }?System.out.println("輸出前三名成績(jī):");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }
??????? }
2019-02-14
public static void main(String[] args) {
???????? int[] scores={89,-23,64,91,119,52,73};
???????? HelloWorld hello=new HelloWorld();
???????? hello.scoresSort(scores,scores.length-2);
???? }
????
???? //定義方法完成成績(jī)排序并輸出前三名的功能
???? public void scoresSort(int[] scores,int num){
???? ? int[] scoresCorrect=new int[num];
???????? Arrays.sort(scores);
???????? int i=0,j=0;
??????? while(i<scores.length){
???????????? if(scores[i]>0 && scores[i]<100){
???????????? ?scoresCorrect[j] = scores[i];
???????????? ?j++;
???????????? ?i++;?
???????????? }else{
???????????? ?i++;
???????????? }??
??????? }?System.out.println("輸出前三名成績(jī):");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }
???????
???????? }