The existing requirements are as follows:
The value of a simple expression is obtained by means of a cmd.
For example, enter Java Expression 3 + 4
The results are: 7
Code:
Import Java.text.DecimalFormat;
public class Expression {
public static void Main (string[] args) throws Exception {
if (args = = NULL | | Args.length! = 3) {
MyException is simply a succession of exception, which can be regarded as exception. (This is because of the special requirements on the topic, please ignore ...) )
throw new MyException ("Incorrect parameter format");
}
Left-hand operand in expression
Double left = double.valueof (Args[0]);
Operator: + 、-、 *,/
String operate = args[1];
Right operand
Double right = double.valueof (args[2]);
Result of Operation
Double result;
Switch (operate) {
Case "+":
result = left + right;
Break
Case "-":
result = Left-right;
Break
Case "*":
result = left * right;
Break
Case "/":
result = Left/right;
Break
Default
throw new MyException ("Incorrect parameter format");
}
System.out.println (New DecimalFormat ("0.00"). Format (result);
}
}
Operation Result:
Found problems with the program only if the operator is an * number
Modify the program to output the args array entered as: 2 * 3 o'clock
if (args! = null) {
for (int i = 0;i < args.length;i++) {
System.out.println (Args[i]);
}
}
found that when the input parameter is ' * ', there is a lot of data in the args array
The comparison found that the names of all the files under the Java file path were duplicated
So, when you pass a value to the Main method, enter the * number to add the names of all the files in the Java file path to the args array.
The question about the * number when Java passes a value to the Main method