public class HelloWorld {? ??? ? //完成 main 方法? ? public static void main(String[] args) {? ? ? ??? ? ? ? // 創(chuàng)建對象,對象名為hello HelloWorld hello = new HelloWorld();?? ? ? ??? ? ? ? // 調(diào)用方法并將返回值保存在變量中? ? ? ??? ? ? ? int maxScore=hello.getMaxAge(); // 輸出最大年齡 System.out.println("最大年齡為:" + maxScore);? } /* * 功能:輸出學(xué)生年齡的最大值?? ? ?* 定義一個無參的方法,返回值為年齡的最大值? ? ?* 參考步驟:? ? ?* 1、定義一個整形數(shù)組 ages ,保存學(xué)生年齡,數(shù)組元素依次為 18 ,23 ,21 ,19 ,25 ,29 ,17? ? ?* 2、定義一個整形變量 max ,保存學(xué)生最大年齡,初始時假定數(shù)組中的第一個元素為最大值? ? ?* 3、使用 for 循環(huán)遍歷數(shù)組中的元素,并與假定的最大值比較,如果比假定的最大值要大,則替換當(dāng)前的最大值? ? ?* 4、使用 return 返回最大值 */ public int getMaxAge() {? ? ? ? int []ages={18,23,21,19,25,29,17};? ? ? ? int maxScore=ages[0];? ? ? ? for(int i=0;i<ages.length;i++){? ? ? ? ? ? if (ages[i+1]>=ages[i]){? ? ? ? ? ? ? ?maxScore=ages[i+1];?? ? ? ? ? ? }? ? ? ? }? ? ? ?? return maxScore;}}? ??
求最大值哪里錯了?。??????
qq_nanranlanW_0
2016-12-17 22:21:05