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