/**
* 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