Java object Simple and practical (calculator case)

Source: Internet
Author: User

The use of objects and properties in Java, methods, and simply wrote a case

1 ImportJava.util.Scanner;2 classCalculste3 {4     intA//define two integers5     intb;6String option;//string that defines the receive operator7      Public voidcount () {8 9         //to judge the operatorTen         Switch(option) { One              Case"+": ASystem.out.println ("Calculate and:" +a+ "+" +b+ "=" + (A +)b)); -              Break; -  the              Case"-": -SYSTEM.OUT.PRINTLN ("Calculation difference:" +a+ "-" +b+ "=" + (A-b)); -              Break; -  +              Case"*": -SYSTEM.OUT.PRINTLN ("Calculated product:" +a+ "*" +b+ "=" + (A *b)); +              Break; A  at              Case"/": -             { -                 //the denominator cannot be 0 for a division operation -                 if(b! = 0) -                 { -System.out.println ("calculator:" +a+ "/" +b+ "=" + "(A/b)); in}Else{ -System.out.println ("The second number you entered cannot be 0, please re-enter"); to                 } +             }     -              Break; the  *              Case"%": $System.out.println ("Calculated remainder:" +a+ "%" +b+ "=" + (a%b));Panax Notoginseng              Break; -  the             default: +System.out.println ("The operator you entered is wrong, please re-enter"); A         } the          +     }  - } $ classDemo3 $ { -     //Requirements: Use the Java class to describe a calculator class, the calculator has the operand 1, the operand 2, the operator three common properties, but also has the function behavior of the computation.  -      Public Static voidMain (string[] args) the     { -SYSTEM.OUT.PRINTLN ("Operation of two numbers");WuyiScanner SCA =NewScanner (system.in); the  -         //to create an object of a class WuCalculste cal =Newcalculste (); -  About         //Assigning a value to a property $System.out.println ("First Number:"); -CAL.A = (int) Sca.nextint (); -  -System.out.println ("Second number:"); ACAL.B = (int) Sca.nextint (); +  theSYSTEM.OUT.PRINTLN ("Input operation symbol:"); -Cal.option = Sca.next ();//methods for accepting characters $          the         //Calling method Operations the Cal.count (); the     } the}

Here are some of the effects on the console:

Momo says: The Java language is an object-oriented programming language, classes, objects in classes, properties and methods of objects are very important

Java object Simple and practical (calculator case)

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.