程序笔记   发布时间:2022-07-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了19、案例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

案例

19、案例

public class MethodDemo11 {
    public static void main(String[] args) {
        judge();
    }

    //加法
    public static double add(double x,double y) {
        return x + y;
    }

    //减法
    public static double sub(double x,double y) {
        return x - y;
    }

    public static double mul(double x,double y) {
        return x * y;
    }

    public static double div(double x,double y) {
        return x / y;
    }

    //判断是哪个方法
    public static void judge() {
        ScAnner sc = new ScAnner(System.in);
        System.out.println("请输入两个数:");
        double x = sc.nextDouble();
        double y = sc.nextDouble();
        System.out.println("请输入运算符(+,-,*,/):");
        String operator = sc.next();
        double result = 0;

        switch ( operator ) {
            case "+":
                result = add(x,y);
                System.out.println(x + "+" + y + "=" + result);
                break;
            case "-":
                result = sub(x,y);
                System.out.println(x + "-" + y + "=" + result);
                break;
            case "*":
                result = mul(x,y);
                System.out.println(x + "*" + y + "=" + result);
                break;
            case "/":
                if ( y == 0 ) {
                    System.out.println("第二个数字不能为0,否则除法运算没有意义");
                } else {
                    result = div(x,y);
                    System.out.println(x + "*" + y + "=" + result);
                }
                break;
            default:
                System.out.println("你输入的是非法字符");
        }
    }
}

输入的不是四个运算符时

19、案例

输入的是 + 时:

19、案例

输入的是 - 时:

19、案例

输入的是 * 时:

19、案例

输入的是 / 时:

19、案例

y 为 0时,输入的是 / 时:

19、案例

大佬总结

以上是大佬教程为你收集整理的19、案例全部内容,希望文章能够帮你解决19、案例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。