Java Basic---Method Advanced (38)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.