public class HelloWorld {
Complete the Main method
public static void Main (string[] args) {
Create object, object named Hello
HelloWorld Hello = new HelloWorld ();
Calling a method and saving the return value in a variable
int Maxscore=hello.getmaxage ();
Maximum output Age
System.out.println ("Maximum Age:" + Maxscore);
}
/*
* Function: Output The maximum age of the student
* Defines an argument-free method that returns a value of the maximum age
* Reference steps:
* 1, define an array ages, save the student age, the array elements are 18, 23, 21, 19, 25, 29, 17
* 2, define a shaping variable max, save the student's maximum age, initially assuming that the first element in the array is the maximum value
* 3, use the For loop to iterate through the elements in the array, and compare with the assumed maximum value, if larger than the assumed maximum, replace the current maximum value
* 4, return the maximum value with return
*/
public int getmaxage () {
Int[] Ages={18, 23, 21, 19, 25, 29, 17};
int maxages = 0;//Save Maximum Value
for (int i=0;i<ages.length;i++)
{
if (ages[i]>maxages)
{
Maxages=ages[i];
}
}
return maxages;
}
}
Operation Result:
Maximum age is: 29
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1548004
Java Basic---Method Advanced (38)