zoukankan      html  css  js  c++  java
  • 简易计算器 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);
            }
        }
    }
    

    运行截图

    代码链接

  • 相关阅读:
    文艺青年、普通青年、2b青年到底是什么意思?
    CMake快速入门教程:实战
    shell脚本中变量$$、$0等的含义
    工作上的C/C++相关
    C/C++的一些备忘
    shell基础二十篇 一些笔记
    C++中this指针的用法详解
    【C++11】新特性——auto的使用
    一个很不错的bash脚本编写教程
    容器
  • 原文地址:https://www.cnblogs.com/cxd20175303/p/10854155.html
Copyright © 2011-2022 走看看