Java programs, writing calculators through scanner

Source: Internet
Author: User

 Packagetest;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;classCalculator {PrivateDouble NUM1; PrivateDouble num2; PrivateString var; PrivateScanner input;  PublicDouble getNum1 () {returnNUM1; }     Public voidsetNum1 (Double num1) { This. NUM1 =NUM1; }     PublicDouble getNum2 () {returnnum2; }     Public voidsetNum2 (Double num2) { This. num2 =num2; }     PublicString GetVar () {returnvar; }     Public voidSetVar (String var) { This. var =var; }     PublicScanner GetInput () {returninput; }     Public voidsetinput (Scanner input) { This. Input =input; }     Public Booleaninput1 () {System.out.println ("Please enter first number"); String Num1str=Input.next (); if(! This. Isnumber (NUM1STR))            {input1 (); }Else{                 This. NUM1 =double.valueof (NUM1STR); }            return true; }     Public BooleanInput2 () {System.out.println ("Enter a second number:"); String Num2str=Input.next (); if(! This. Isnumber (NUM2STR))        {Input2 (); }Else if(Var.equalsignorecase ("/") &&num2str.equalsignorecase ("0")) {System.err.println ("Divisor cannot be 0");        Input2 (); }Else{             This. num2 =double.valueof (NUM2STR); }        return true; }     Public BooleanInputvar () {System.out.println ("Input calculation symbol: +-*/"); String Varstr=Input.next (); Pattern Pat=pattern.compile ("[+-\\\\*/]"); Matcher Match=Pat.matcher (VARSTR); Booleanflag=match.matches (); if(flag==false) {System.err.println ("The operation symbol is incorrect!" ");         Inputvar (); }Else{              This. SetVar (VARSTR); }                 return true; }         Public voidgocal () {Scanner input=NewScanner (system.in);  This. SetInput (input); Booleanflag1= This. INPUT1 (); if(FLAG1) {if( This. Inputvar ()) {                if( This. Input2 ())                {Calculate (); }            }        }    }     Public voidcalculate () {Try {            Switch(Var) { Case"+": System.out.println (NUM1+ "+" +num2+ "=" + (num1+num2));  Break;  Case"-": System.out.println (NUM1+ "-" +num2+ "=" + (num1-num2));  Break;  Case"*": System.out.println (NUM1+ "-" +num2+ "=" + (num1*num2));  Break;  Case"/": System.out.println (NUM1+ "/" +num2+ "=" + (num1/(Double) (num2)) ;  Break; default:                 Break; }        } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); if(input!=NULL) {input.close (); System.exit (0); }        }        if(input!=NULL) {input.close (); System.exit (0); }           }     Public Booleanisnumber (Object obj) {if(obj==NULL|| Obj.tostring (). Equals ("")){            return false; } Pattern Pattern=pattern.compile ("[0-9]+.*[0-9]*"); String Str=obj.tostring (); Matcher Matcher=Pattern.matcher (str); Booleanflag=matcher.matches (); if(flag==false) {System.err.println ("Please enter a valid value!" "); }        returnFlag; }} Public classTESTB { Public Static voidMain (string[] args) {Calculator cal=NewCalculator ();                    Cal.gocal (); }}

Java programs, writing calculators through scanner

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.