要求通过定义无参带返回值的方法来实现,返回值为最大年龄 2
2016-03-05 14:07
225 查看
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;
}
//完成 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;
}
相关文章推荐
- Group by与having理解
- Ubuntu jdk报Picked up JAVA_TOOL_OPTIONS信息解决
- c语言实现拓扑排序(《数据结构》算法7.12)
- app性能优化之Time Profiler工具的使用
- 寻找Coder
- consistent hashing
- MOS上oracle升级补丁查询
- knockoutjs十二 textInput绑定
- ioctl的原理及实现
- MySQL优化技巧之三(索引操作和查询优化)
- linux下面自动打包备份并且删除5 天前的文件
- linux分页机制
- poj--3061--Subsequence(贪心)
- mac 下 nginx的安装
- mysql日志详细解析
- poj--3061--Subsequence(贪心)
- 对于软件工程存在的疑问
- jQuery选择器 大于 空格 波浪线 加号
- opacity与alpha的区别
- Maven是什么,以及为什么要使用Maven