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