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)