搞不懂啊,想了半天也沒搞懂
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.post(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void post(int[] nums){
? ? ? ? for(int num:nums){
? ? ? ? ? ? if(num>100 || num<0){
? ? ? ? ? ? ? ? num = 0;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Arrays.sort(nums);
? ? ? ? System.out.println("考試成績(jī)的前三名為");
? ? ? ? for(int i=nums.length-1; i>nums.length-4; i--){
? ? ? ? ? ? System.out.println(nums[i]);
? ? ? ? }
? ? } ?
}
這里我用了foreach循環(huán),但是怎么數(shù)組里面的負(fù)數(shù)和大于100的數(shù)還在啊?看了看別人的代碼,也是這樣寫的啊
2015-07-24
樓組,你給num賦值的時(shí)候并沒有改變數(shù)組中的值呀,num只是一個(gè)臨時(shí)變量呢
如果樓組非要用for each循環(huán)的話,可以這樣改: