Java:从if语句转换为方法

时间:2017-11-07 20:59:53

标签: java

我很难将if语句转换为不同的方法。我试图将它转换为4个单独的方法:add,sub,mul和div,它必须在CMD中声明,但我不能以某种方式做到。我需要帮助和谢谢。

public static void main(String[] args){
    int first   = Integer.parseInt(args[1]);
    int second  = Integer.parseInt(args[2]);
    int third   = Integer.parseInt(args[3]);
    int forth   = Integer.parseInt(args[4]);

    if(args[0].equals("add")){
        if (second == 0 || forth == 0){
            System.out.println("Undividable by zero");
        } else if (second == forth){
            System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth);
        } else {
            System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first * forth + second * third) + "/" + (second * forth));
        }
    } else if (args[0].equals("sub")){
        if (second == 0 || forth == 0){
            System.out.println("Undividable by zero");
        } else if (second == forth){
            System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first + third) + "/" + forth);
        } else {
            System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first * forth - second * third) + "/" + (second * forth));
        }
    } else if (args[0].equals("mul")){
        if (second == 0 || forth == 0){
            System.out.println("Undividable by zero");
        } else {
           System.out.println(first + "/" + second + " * " + third + "/" + forth + " = " + (first * third ) + "/" + (second * forth));
        }
    } else if (args[0].equals("div")){
        if (second == 0 || forth == 0){
            System.out.println("Undividable by zero");
        } else  {
            System.out.println(first + "/" + second + " : " + third + "/" + forth + " = " + (first * forth) + "/" + (second * third));
        }
    } else {
        System.out.println("Recheck the code");
    }        
}

3 个答案:

答案 0 :(得分:0)

或者如果你想制作一种方法 public void this_is_your_method(int n){...}

我假设你遇到的问题是因为System.out.println,在制作方法时你需要说明你想要返回的是什么类型,因为你想打印一些你会说无效的东西。你不想打印任何东西只是打印这个并且我写了int以使你的工作更容易,因为写一次所有代码比多次更好只需将args [0]更改为args [n]

答案 1 :(得分:0)

示例

<强> Main.Class

public static void main(String[] args) {
    int first   = Integer.parseInt(args[1]);
    int second  = Integer.parseInt(args[2]);
    int third   = Integer.parseInt(args[3]);
    int forth   = Integer.parseInt(args[4]);

    if(args[0].equals("add")){
        add(first,second,third,forth);
    } ...
}

public static void add(int first, int second, int third, int forth) {
    if (second == 0 || forth == 0){
        System.out.println("Undividable by zero");
    } else if (second == forth){
        System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth);
    } else {
        System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first * forth + second * third) + "/" + (second * forth));
    }
}

答案 2 :(得分:0)

当有很多if then else时你也可以选择switch-case。

以下是如何将代码模块化为方法...

我也在分享如何使用lambda函数(java 8)。

interface MathOperation {
    public void operate(int first, int second, int third, int forth);
}

public class StackOverflow {

    public static void main(String[] args) {
        int first = Integer.parseInt(args[1]);
        int second = Integer.parseInt(args[2]);
        int third = Integer.parseInt(args[3]);
        int forth = Integer.parseInt(args[4]);

        MathOperation addition = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else if (b == d) System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + (a + c) + "/" + d); else System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + (a * d + b * c) + "/" + (b * d));};
        MathOperation subtraction = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else if (b == d) System.out.println(a + "/" + b + " - " + c + "/" + d + " = " + (a + c) + "/" + d); else System.out.println(a + "/" + b + " - " + c + "/" + d + " = " + (a * d - b * c) + "/" + (b * d));};
        MathOperation multiplication = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else System.out.println(a + "/" + b + " * " + c + "/" + d + " = " + (a * c) + "/" + (b * d));};
        MathOperation division = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else System.out.println(a + "/" + b + " : " + c + "/" + d + " = " + (a * d) + "/" + (b * c));};

        switch(args[0]) {
            case "add" :
                add(first, second, third, forth);
                addition.operate(first, second, third, forth);
                break;
            case "sub":
                sub(first, second, third, forth);
                subtraction.operate(first, second, third, forth);
                break;
            case "div":
                div(first, second, third, forth);
                division.operate(first, second, third, forth);
                break;
            case "mul":
                multiply(first, second, third, forth);
                multiplication.operate(first, second, third, forth);
                break;
            default :
                System.out.println("Recheck the code");
        }
    }

    private static void multiply(int first, int second, int third, int forth) {
        if (second == 0 || forth == 0) {
            System.out.println("Undividable by zero");
        } else {
            System.out.println(first + "/" + second + " * " + third + "/" + forth + " = " + (first * third) + "/"
                    + (second * forth));
        }
    }

    private static void div(int first, int second, int third, int forth) {
        if (second == 0 || forth == 0) {
            System.out.println("Undividable by zero");
        } else {
            System.out.println(first + "/" + second + " : " + third + "/" + forth + " = " + (first * forth) + "/"
                    + (second * third));
        }
    }

    private static void sub(int first, int second, int third, int forth) {
        if (second == 0 || forth == 0) {
            System.out.println("Undividable by zero");
        } else if (second == forth) {
            System.out.println(
                    first + "/" + second + " - " + third + "/" + forth + " = " + (first + third) + "/" + forth);
        } else {
            System.out.println(first + "/" + second + " - " + third + "/" + forth + " = "
                    + (first * forth - second * third) + "/" + (second * forth));
        }
    }

    private static void add(int first, int second, int third, int forth) {
        if (second == 0 || forth == 0) {
            System.out.println("Undividable by zero");
        } else if (second == forth) {
            System.out.println(
                    first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth);
        } else {
            System.out.println(first + "/" + second + " + " + third + "/" + forth + " = "
                    + (first * forth + second * third) + "/" + (second * forth));
        }
    }
}