Lucky Cat Tutoring Services


Return Method Example
October 20, 2009, 4:08 pm
Filed under: Uncategorized | Tags: , , , , , ,

Here’s an example of a method that returns the smallest number out of a set of three entered by the user:

/*

*making methods return
*This program demonstrates the use of value return methods
*get three integers and return the smallest
*/
import java.util.Scanner; // Needed for the Scanner class

public class methodReturn2
{

public static int findSmall(int n1, int n2,int n3)//this method returns an int and picks the bigger one

{
if(n1 < n2 && n1 < n3)
{
return n1;
}
else if(n2 < n1 && n2< n3)
{
return n2;
}
else if(n3 < n1 && n3″);
int number1=sc.nextInt();
System.out.println(“Please enter the second integer–>”);
int number2=sc.nextInt();
System.out.println(“Please enter the third integer–>”);
int number3=sc.nextInt();

System.out.println(“\nThe smallest of the values is ” +findSmall(number1,number2,number3));
//invoke method and give it ints to work with

}

}

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: