為什么用while結(jié)果和用if不一樣
import java.util.Arrays;
public class method2 {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? method2 hello=new method2();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? hello.sortScore(scores);? ??? ? ? ?
? ? }
? ??? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void sortScore(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){//****************這里用while為什么結(jié)果不一樣?********
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ?} ??
? ? ? ? ? ??
? ? ? ? ? ??
? ? ? ? }
? ? }
}
2015-12-23
第一:你用使用if判斷結(jié)束for循環(huán),程序是完全正確的;
第二:若將if換成while,則for循環(huán)里嵌套了一個while循環(huán),
第三:break語句位于while循環(huán)體內(nèi)則代表結(jié)束while循環(huán),
第四:while循環(huán)判斷語句為count>=3,僅當滿足條件while退出,
第五:while循環(huán)退出之后等于沒有,此時for循環(huán)做的事就是遍歷并輸出排序后的符合第一個if條件下的值
2015-12-23
而且你的這個代碼最后面多了一個大括號
2015-12-23
while里用 break 是跳出 while循環(huán) ,用 if才會是 跳出? for循環(huán)。
2015-12-23
if是判斷語句,while是循環(huán)語句你搞混了啊