简易计算器 20175303

简易计算器 20175303

题目要求

提交测试码云链接和测试截图(测试不要和下面的示例相同),加上学号信息

实现一个简易计算器Calc,支持+ - x / 和%运算, 从命令行传入计算数据,比如:

java Calc 2 + 3 结果为 2 + 3 = 5
java Calc 8 - 3 结果为 8 - 3 = 5
java Calc 2 x 3 结果为2 x 3 = 6
java Calc 10 / 2 结果为10 / 2 = 5
java Calc 10 % 3 结果为10 % 3 = 1

代码

import java.lang.String;
/**
 * Demo class
 *
 * @author cxd20175303
 * @date 2019/5/12
 */
public class Calc {
    public static void main(String[] args) {
        int result = 0;
        if (args.length != 3) {
            System.out.println("Usage: java Calc operato1 operand(+ - x / %) operator2");
            return;
        }
        else {
            if (args[1].equals("+")) {
                result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
            }
            if (args[1].equals("-")) {
                result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
            }
            if (args[1].equals("x")) {
                result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
            }
            if (args[1].equals("/")) {
                if (args[2].equals("0")) {
                    System.out.println("Denominator cannot be zero");
                    return;
                }
                else {
                    result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
                }
            }
            if (args[1].equals("%")) {
                result=Integer.parseInt(args[0])%Integer.parseInt(args[2]);
            }
            System.out.println(args[0] + " " + args[1] + " " + args[2] + " = " + result);
        }
    }
}

运行截图

代码链接

猜你喜欢

转载自www.cnblogs.com/cxd20175303/p/10854155.html