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)