為什么我的代碼沒有跳出循環(huán),我要的前三名但是總輸出五個
import java.util.Arrays;
public class HelloWorld {
??? public static void main(String[] args) {
?? ??? ?HelloWorld hello = new HelloWorld();
?? ?int []scores = {89,-23,64,91,119,52,73};
?? ?System.out.println("考試成績的前三名為:");
??????? hello.print(scores);
?? ?}
??? public void print(int []scores) {
?? ??? ?Arrays.sort(scores);
?? ??? ?for(int i = scores.length - 1;i >=0;i--) {
?? ??? ??? ?int count = 0;
?? ??? ??? ?if(scores[i]<0 || scores[i]> 100){
?? ??? ??? ?continue;? ?
?? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ???? count ++;
?? ??? ??? ?if(count > 3){
?? ??? ???????? break;
?? ??? ???? }
?? ??? ???? System.out.println(scores[i]);
?? ??? ?}
?? ??? ?
??? }
}
2019-05-24
int count=0;你放在for循環(huán)里面,循環(huán)的時候每次都初始化為0,那就永遠(yuǎn)不可能大于3