Java method "practice", java method practice

Source: Internet
Author: User

Java method "practice", java method practice

1. Define a method and call it. This will be convenient and greatly reduce unnecessary waste. But before defining a method, you must first specify the final result of the method to be executed? It is also necessary to determine whether some parameters (parameters) are required in this method)

For example, the following code snippet:

1 // enter the taxi rental System 2 public void YesOrNo () {3 System. out. println ("***********************"); 4 System. out. print ("\ n"); 5 System. out. println ("do you want to rent a car? 1. Yes 2. No "); 6 bytes input = new bytes (System. in); 7 if (input. nextInt () = 1) {8 System. out. println ("************* type of your rental car and its price list ***********"); 9} 10}View Code

The preceding YesOrNo () method only requires a prompt to enter the system. No parameters or values need to be returned. You can directly call it in the main function.

For example, the following code snippet:

1 // get Car information 2 public void getCarsList (Car [] cars) {3 System. out. printf ("No. \ t car name \ t rental \ t capacity"); 4 for (int I = 1; I <= cars. length; I ++) {5 System. out. println ("\ r" + I + ". \ t "+ cars [I-1]. getBrand () + "\ t" + cars [I-1]. getPrice () + "RMB/day \ t" 6 + cars [I-1]. getCapacity (); 7} 8}View Code

The preceding getCarsList () method prints the vehicle information. It needs to input the original vehicle information data (Car [] cars), which requires defining a parameter method. But it does not need to return any value. You can directly call it in the main function:

 

2. But in fact, the method must not only define the "execution action", but also return the "processed" data to the main function. For example, the following snippet:

1 // calculate the amount 2 public int getMoneys (Car [] cars, int money, int carNum) {3 money = cars [carNum-1]. getPrice (); 4 return money; // return the value of money 5}View Code

The preceding getMoneys () method calculates the amount. It not only needs to pass in the original data (Vehicle Information), but also needs to return the amount (return money) to print the bill in the main function.

3. The content of the value in the method is the most basic and important. Today, I am confused. Finally, I am able to "inquire from multiple parties" and finally get it done.

 

The above is from Java programming language version 4th.

Send a blog: http://www.jb51.net/article/39970.htm, which provides detailed details.

Additional test:

1 public class ArgsTransfer {2 public static void main (String [] args) {3 // TODO Auto-generated method stub 4 ArgsTransfer t = new ArgsTransfer (); 5 int a = 5; 6 int B = 10; 7 8 // 1. input values to real parameters a and B, pass the parameters I and j 9 // 2, object t calls the swap Method 10 // 3, and the I and j Values in the method swap are interchangeable, and return the value of I (return I;) to the a11 System in the main function. out. println ("a =" + a + "B =" + B + "a =" + t. swap (a, B); 12} 13 14 public int swap (int I, int j) {// The method can only perform the action and cannot pass the value (up) 15 int temp = I; 16 I = j; 17 j = temp; 18 System. out. println ("swap method executed"); 19 System. out. println ("Here I =" + I + ", j =" + j + ", I exchanged with j"); 20 System. out. println ("and has nothing to do with a, B in main method"); 21 22 return I; 23} 24}View Code

Running result:

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.