為什么輸出結(jié)果不同?
import java.util.Arrays; public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ?int scores[]=new int[]{89,-23,64,91,119,52,73}; ?
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? System.out.println("前三名的成績?yōu)?");
? ? ? ? hello.score(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void score(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num=0;
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }num++;
? ? ? ? ? ? if(num>=3){
? ? ? ? ? ? ? ? System.out.println(scores[i])
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ??
? ? }
? ??
? ??
-------------------------------------------------------------------------------------
import java.util.Arrays; public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ?int scores[]=new int[]{89,-23,64,91,119,52,73}; ?
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? System.out.println("前三名的成績?yōu)?");
? ? ? ? hello.score(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void score(int [] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num=0;
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }num++;
? ? ? ? ? ? if(num>3){
? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? }}
? ??
? ??
? ??
2017-08-11
第一種方法在num>=3時才打印輸出數(shù)據(jù),這樣剛好把前三名給去掉了(不打?。?,即打印輸出前三名以后的數(shù)據(jù),直達最后。第二種方法在num>3時用break跳出 for循環(huán),這樣只能打印出前三名