Java object Simple and practical (calculator case)

Source: Internet
Author: User
Tags sca

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

 1 Import Java.util.Scanner;  2 class Calculste 3 {4 int A;//define two integers 5 int b; 6 string option;//The string that defines the receive operator 7 public void count () {8 9//The operator is judged by the option {one case "+": System.out.println ("Calculation and:" +a+ "+" +b + "=" + (a+b)); break;14 case "-": System.out.println ("Calculation difference:" +a+ "-" +b+ "=" + (A-B)             ); break;18 "*": System.out.println ("Calculated product:" +a+ "*" +b+ "=" + (a*b)); 21 Break;22 "/": 24 {25//For division, denominator cannot be 0 if (b! = 0)                     System.out.println ("calculator:" +a+ "/" +b+ "=" + (A/b) ");}else{30              System.out.println ("The second number you entered cannot be 0, please re-enter");}32} break;34 35     Case "%": System.out.println ("calculated remainder:" +a+ "%" +b+ "=" + (a%b)); Notoginseng break;38 39        Default:40 System.out.println ("You entered the operator is wrong, please re-enter");}42-}45 class Demo 3 46 {47//requirement: 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 computational function behavior.  N-public static void main (string[] args) {System.out.println ("Two-number operation"); Wuyi Scanner SCA = new Scanner (system.in); 52 53//Create Object of class Calculste cal = new Calculste (); 55 56//Assign value to attribute Syste M.out.println ("First Number:"), CAL.A = (int) sca.nextint (); System.out.println ("Second number:"); cal.b = (int) sca.nextint (); System.out.println ("Input operation symbol:"); cal.option = Sca.next (); Method of accepting Characters 65 66//Call Method Operation Cal.count (); 68}69}

Here are some of the effects on the console:

Java object Simple and practical (calculator case)

Related Article

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.