Requires that a calculation expression be entered in the console by entering the calculation result

Source: Internet
Author: User

/**
* Requires a calculation expression to be entered in the console:
For example
* 51+52 (Subtraction method)

Press ENTER to calculate the result:
* 103
*
* A+1

Press ENTER to make the following prompt:
* Not a mathematical calculation of an expression
*

The following are available for study
* 1+2*3+ (3+4) =
*
* 3*5+1/2

*/

 Public classWork01 { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter a calculation expression:"); String Line=Scanner.nextline (); //the position of the operator        intindex = 0; if(index = line.indexof ("+")) >0) {print (Index,line,1); }Else if(index = Line.indexof ("-")) >0) {print (Index,line,2); }Else if(index = Line.indexof ("*")) >0) {print (Index,line,3); }Else if(index = Line.indexof ("/")) >0) {print (Index,line,4); }    }    /**     *      * @paramIndex *@paramType 1 plus 2 minus 3 times 4 divide*/     Public Static voidPrintintIndex,string Line,inttype) {            intA=Integer.parseint (line.substring (0, index)); intb=Integer.parseint (line.substring (Index+1)                    ); if(type==1) {System.out.println (a+ "+" +b+ "=" + (A +b)); }Else if(type==2) {System.out.println (a+ "-" +b+ "=" + (A-b)); }Else if(type==3) {System.out.println (a+ "*" +b+ "=" + (A *b)); }Else if(type==4) {System.out.println (a+ "/" +b+ "=" + (A/b)); }    }
View Code

Requires that a calculation expression be entered in the console by entering the calculation result

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.