20145321 Experimental One experimental report

Source: Internet
Author: User

20145321 Experiment One experiment report experiment name

Familiarity with the Java development environment

Experimental content

Implement the arithmetic and test

Experimental steps

1. I made a simple arithmetic, that is, there are only two subtraction numbers.

2. Create a scanner instance to enter two numbers, DecimalFormat class to keep the number formatted two digits after the decimal point.

3. Code:

Import Java.util.scanner;import Java.text.decimalformat;public class SiZe {public static void main (string[] args) {SiZ        e size=new size ();        Scanner Scanner = new Scanner (system.in);            try {System.out.print ("Please enter the first number:");            Double x = scanner.nextdouble ();            System.out.print ("Please enter a second number:");            Double y = scanner.nextdouble ();            System.out.print ("Please enter operator:");            String s = scanner.next ();            Char z = s.charat (0);        Size.yunsuan (x, y, z);        } catch (Exception e) {System.out.println ("Please enter the correct data");    }}public static void Yunsuan (double x,double y,character z) {decimalformat r=new decimalformat ();    R.applypattern ("#0.00");    if (z.equals (' + ')) {System.out.println (x+ "+" +y+ "=" +r.format ((X+y)));    }else if (z.equals ('-')) {System.out.println (x+ "-" +y+ "=" +r.format ((x-y)));    }else if (z.equals (' * ')) {System.out.println (x+ "*" +y+ "=" +r.format ((X*y))); }else if (z.equals ('/'){if (y = = 0) {System.out.println ("dividend cannot be 0");        } else {System.out.println (x + "/" + y + "=" + R.format ((x/y)));    }}else {System.out.println ("unrecognized operator"); }}}
Problem solving

1. When the two-digit position entered a non-numeric error, you can use the eighth chapter exception handling method, use try and catch to remind the user.

2. When the position of the operation symbol is entered into something else, we cannot do arithmetic, so we also need to remind the user.

3. The experiment ignores 0 cannot do dividend, out of the question. It was only later that I thought of the rule.

Run results

20145321 Experimental One experimental report

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.