Javase Programming Fundamentals 5

Source: Internet
Author: User

Javase Programming Fundamentals 5

Arrays and methods;

Syntax: Access modifier symbol return type method name () {

Method body;

}

Note: (1), method to be directly defined in class;

(2), the design method is designed to achieve the reuse of functions;

First, define the declaration method;

1 , write the method directly within class:

Cases:

 Public void checkIn () {System. out. println (" breaking Through the night sky, stepping into the dawn ");}

Type:

Void (no parameters, no return results), Int,string,float,double,boolean;

2 , at main entrance:

A variable defined by a class becomes an object;

Syntax: Class name variable (object) = new class name ();

New is to open up a new space;

Method invocation: Variable (object). Method name ();

3. naming rules for full names:

(1) , letters, underscores, numbers, $; cannot begin with numbers;

(2) , package name: must all be lowercase letters;

(3) , file name: initials and the first letter of the following English words should be capitalized;

(4) , variable and method name: first letter lowercase, the following English word capitalization;

4. call a method principle with parameters:

(1) , the number of parameters should be consistent;

(2) , type to be consistent;

(3) , the order should be consistent;

(4) , the parameter has the function of passing;

(5) and two parameters separated by commas (,);

(6) , passing the data given at the time of invocation to the running method;

Second, types of methods;

1. no parameters, no return results;

Example:1, no parameters, no return results;

Method is written to:

 Public void checkIn () {      System. out. println (" breaking Through the night sky, stepping into the dawn ");   }

To run the program's write:

     // New opens up a space;   New Person ();        Liming.checkin ();

2, there are parameters, no return results;

Example: There are parameters, no return results;

Write method within person;

 Public void checkwn (int  ID) {      if(id==1) {         System. out. println (" sign in ");      } Else {         System. out. println (" sign in ");      }   }

Write the running program in Exam_1;

// New opens up a space;  New person (); Liming.checkwn (N);

3. having parameters, having the result of returning;

Example: There are parameters, there are return results;

Write the method in public;

   Public int numberadd (int n1,int  n2) {      return n1*n2;   }

Write the running program in Exam_1;

int i = liming.numberadd (35); System. out. println (" the product of two numbers is:"+i);

4, texting example;

To write a method in person:

 Public void duanxin () {      new Scanner (System.  in);      System. out. println (" Please enter the contents of the text message:");       = Input.next ();      System. out. println (" input content is:"+Sun);   }

Write the running program in Exam_1;

 

 // New opens up a space; New person (); liming.duanxin;    

5, judge the user login example:

Write the method in person;

 Public boolean Yonghu (String username,string password) {      if(Username.equals ("Qizhi  ") && password.equals ("1763")         {return  true;      } Else {         returnfalse;      }}

Write the running program in Exam_1;

Scanner input =NewScanner (System.inch); String Username,password;boolean Flag; System. out. println ("Welcome to the Mini DVD system"); System. out. println ("Please enter user name:"); Username=Input.next (); System. out. println ("Please enter your password:"); Password=Input.next (); Flag=Liming.yonghu (Username,password); if(Flag = =true) {System. out. println ("User Login Successful"); }Else{System. out. println ("user logon Failed "); }   }

6, to find two numerical methods and examples;

To write a method in Calcalator:

 Public int calc (intint  b) {return a +b;}

Write the running program in Test_1:

Scanner input = new Scanner (system.in); int n1,n2,sum; System.out. Printin ("Please enter the first value:"); n1 = input.nextint (); System.out. println ("Please enter a second value:"); n2 = Input.nextint ();

To open up a new space;

Calc jsq = new Calc ();

Call method;

sum = Jsq.calc (N1,N2);

System.out.printl (String.Format ("%d+%d=%d", N1,n2,sum));

Javase Programming Fundamentals 5

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.