Experiment four-unit test

Source: Internet
Author: User

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

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.