ImportJava.util.*; Public classJisuan { Public Static voidMain (string[] args) {System.out.println ("Operation symbol bit: +,-,*,/"); Scanner SC=NewScanner (system.in); System.out.println ("Please enter the first number:"); String Str=Sc.nextline (); System.out.println ("Please enter the second number:"); String str2=Sc.nextline (); System.out.println ("Please enter operator:"); String STR3=Sc.nextline (); Try{ DoubleA=double.parsedouble (str); Doubleb=double.parsedouble (STR2); Core Core=NewCore (); Core.fengzhuan (STR3, A, b); }Catch(NumberFormatException e) {System.out.println ("Input error, please re-enter"); Core Core=NewCore (); System.out.println ("Please enter the first number:"); String STR4=Sc.nextline (); System.out.println ("Please enter the second number:"); String STR5=Sc.nextline (); DoubleA=double.parsedouble (STR4); Doubleb=double.parsedouble (STR5); Core.fengzhuan (STR3, A, b); } finally{} }}classCore {Scanner g=NewScanner (system.in); Public voidFengzhuan (String op,DoubleADoubleb) { if(Op.equals ("+") {System.out.println (a+b); }Else if(Op.equals ("-") {System.out.println (a-C); }Else if(Op.equals ("*") {System.out.println (a*b); }Else if(Op.equals ("/")){ if(b==0) {System.out.println ("Operation error Please re-enter"); System.out.println ("Please enter the value of B:"); b=g.nextdouble (); System.out.println (A/b); } Else if(b!=0) {System.out.println (a/b); } } Else{String str3; System.out.println ("The symbol input is wrong, please re-enter"); STR3=G.next (); Jisuan2 ji=Newjisuan2 (); Ji.huansuan (STR3, A, b); } }}classJisuan2extendsjisuan{Scanner SC=NewScanner (system.in); Public voidHuansuan (String Str3,DoubleADoubleb) { if(Str3.equals ("+") {System.out.println (a+C); }Else if(Str3.equals ("-") {System.out.println (a-b); }Else if(Str3.equals ("*") {System.out.println (a*b); }Else if(Str3.equals ("/")){ if(b==0) {System.out.println ("Operation error Please re-enter"); System.out.println ("Please enter the value of B:"); b=sc.nextdouble (); System.out.println (A/b); } Else if(b!=0) {System.out.println (a/b); } } }
My idea of this procedure is to write a simple arithmetic first and then test it successfully. Then is to solve the direction of the anomaly, first solve the number of anomalies, through the exception catch and then let the user input again, so that the operation is correct, the last is the input symbol of the exception, I am through the If-else statement to solve, if the judgment symbol is not +,-*,/and then the user re-input again. This program let me review the Java exception catch, let me how to deal with the exception more understanding.
Experiment four-unit test