麻煩大佬幫忙看看問(wèn)題出在哪了
為什么我最后輸出的是最小的三個(gè),而且該剔除的數(shù)據(jù)沒(méi)有剔除。。。。在線求大佬解答
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("考試成績(jī)的前三名為:");?? ?
??? hello.topThree(scores);?? ?
? ?
??? }
??? //定義方法完成成績(jī)排序并輸出前三名的功能
??? public void topThree(int[] scores){
??????? Arrays.sort(scores);
??????? for(int i=scores.length-1;i>=0;i--){
??????????? if(scores[i]<0||scores[i]>100)
??????????? continue;
??????? }
?? ?
?????? ?
?? ?
??????? for(int j=0;j<=2;j++){
??????????? System.out.println(scores[j]);
??????? }
??? }
? ?
}
2020-07-07
大佬,要不這樣:
package java12;
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("考試成績(jī)的前三名為:");? ??
? ? hello.topThree(scores);? ? ? ?
? ? }
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void topThree(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int count = 0;
? ? ? ? for(int i = scores.length - 1;i >= 0;i--){
? ? ? ? ? ? if(scores[i] < 0 ||scores[i] > 100)
? ? ? ? ? ? {
? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? count++;
? ? ? ? ? ? if(count >= 3) break;
? ? ? ? ? ? }
? ? ? ? } ? ??
? ? }
}
2020-07-07
哈哈,我竟然看懂了
2020-07-06
首先continue是跳出本次循環(huán),那么你第一個(gè)for就毫無(wú)用處,你可以設(shè)置為超過(guò)限制的數(shù)變成一個(gè)負(fù)數(shù),比如負(fù)一(如果是找最小的幾個(gè)數(shù),那么定義為大于100的數(shù),比如101)
另外,sort是從小到大排序,所以如果你想找到最小,那么可以直接從scores[0]開(kāi)始,采用i++,去找最小的三個(gè),但是如果想找最大的,可以從scores.length - 1開(kāi)始,采用i--去找最大的三個(gè)(當(dāng)然還有個(gè)笨辦法是創(chuàng)建一個(gè)新數(shù)組,讓新數(shù)組的length-1等于scores[0],以此類推)
2020-07-01
sort是從小到大排序,所以你輸出的是最小的3個(gè)。
而且continue的作用只是跳出本次循環(huán),不是剔除數(shù)據(jù)。