要求通过定义无参带返回值的方法来实现,返回值为最大年龄 2

public class HelloWorld {
    
    //完成 main 方法
    public static void main(String[] args) {
        
        // 创建对象,对象名为hello
HelloWorld hello = new HelloWorld(); 
        

        // 调用方法并将返回值保存在变量中
        int maxScore = hello.getMaxAge();
        
// 输出最大年龄
System.out.println("最大年龄为:" + maxScore); 
}


/*
* 功能:输出学生年龄的最大值 
     * 定义一个无参的方法,返回值为年龄的最大值
     * 参考步骤:
     * 1、定义一个整形数组 ages ,保存学生年龄,数组元素依次为 18 ,23 ,21 ,19 ,25 ,29 ,17
     * 2、定义一个整形变量 max ,保存学生最大年龄,初始时假定数组中的第一个元素为最大值
     * 3、使用 for 循环遍历数组中的元素,并与假定的最大值比较,如果比假定的最大值要大,则替换当前的最大值
     * 4、使用 return 返回最大值
*/
public int getMaxAge() {

    int[] stuAge = {18,23,21,19,25,29,17};
    int max = 0;
for(int i:stuAge)
    {
      max=max>i?max:i;
    }
    return max;
}

}

for (int age : ages){
if (age > max)
max = age;
}




public int getMaxAge() {
        //装了个波一啊
   int[] ages = {18,23,21,19,25,29,17};
        Arrays.sort(ages);
        int max = ages[ages.length-1];
        return max;    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章