Java learning: methods

Source: Internet
Author: User

Method is placed in two different Java files, one refers to the storage of the corresponding business logic,

Another Java file is to control only the input and output (that is, the user interface).

Calc Method:

/** Method*/ public classCalc {//method identifiers to note: static and type of method double     public Static DoubleAddDoubleADoubleB) {        Doubler = A +b; returnr; }         public Static DoubleSubDoubleADoubleB) {        Doubler = a-b; returnr; }         public Static DoubleMulDoubleADoubleB) {        DoubleR = A *b; returnr; }         public Static DoubleDivDoubleADoubleB) {        Doubler = a/b; returnr; }}

Test method (user interface):

Importjava.util.Scanner; public classTestcalc public Static voidmain (String Args[]) {//Accept DataScanner sc =NewScanner (system.in); System.out.println ("num1:"); DoubleNUM1 =sc.nextdouble (); //Accept operatorSystem.out.println ("opp:"); String opp=Sc.next (); //char opp1 = Opp.charat (index);System.out.println ("num2"); Doublenum2 =sc.nextdouble (); Doubled = 0; //invoke the appropriate business logic        Switch(opp)) {            //note: The switch statement does not support string type parameters before the JDK7 Version.              case"+": D=Calc.add (num1,num2);  break;  case"-": D=calc.sub (num1,num2);  break;  case"*": D=Calc.mul (num1,num2);  break;  case"/": D=Calc.div (num1,num2);  break; default: System.out.println ("errormessage!");  break; }    }}

Java learning: methods

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.