为什么我班级的数学方法归零?

时间:2015-07-29 12:50:34

标签: java methods return

我是Java新手,我目前正在玩方法和类。我已经为计算做了一个小程序,但我的乘法和除法方法总是返回零。以下是课程:

操作员类:

public class Operators {

    int add(int x, int y) {

        int sum;

        sum = x + y;

        return sum;

    }

    int sub(int x, int y) {

        int sum;

        sum = x - y;

        return sum;

    }

    int multiply(int x, int y) {

        int sum;

        sum = x * y;

        return sum;

    }

    int divide(int x, int y) {

        int sum;

        sum = x / y;

        return sum;

    }

    double sqrt(double x) {

        double sum;

        sum = Math.sqrt(x);

        return sum;

    }
}

菜单类:

public class Menu {

    Operators operator = new Operators();

    int a, b, addResult;
    int c, d, subResult;
    int e, f, mulResult;
    int g, h, divResult;
    double i, sqrResult;

    Scanner inndata = new Scanner(System.in);


    void chooseOperator(int what) {

        switch (what) {

        case '1' : 
            System.out.println("Type in the first number: ");
            a = inndata.nextInt();

            System.out.println("Type in the number you want to add: ");
            b = inndata.nextInt();

            addResult = operator.add(a,b);

            System.out.println(a + " + " + b + " = " + addResult);
        break;

        case '2' : 
            System.out.println("Type in the first number: ");
            c = inndata.nextInt();

            System.out.println("Type in the number you want to subtract: ");
            d = inndata.nextInt();

            subResult = operator.sub(c,d);

            System.out.println(c + " - " + d + " = " + subResult);  
        break;

        case '3' : 
            System.out.println("Type in the first number: ");
            e = inndata.nextInt();

            System.out.println("Type in the number you want to multiply the first number with: ");
            f = inndata.nextInt();

            subResult = operator.multiply(e,f);

            System.out.println(e + " * " + f + " = " + mulResult);  
        break;

        case '4' : 
            System.out.println("Type in the first number: ");
            g = inndata.nextInt();

            System.out.println("Type in the number you want to dive the first number with: ");
            h = inndata.nextInt();

            subResult = operator.multiply(g,h);

            System.out.println(g + " / " + h + " = " + divResult);  
        break;

        case '5' : 
            System.out.println("Type in the number you want to square: ");
            i = inndata.nextInt();

            sqrResult = operator.sqrt(i);

            System.out.println("The square root of " + i + " is " + sqrResult);
        break;

        }       
    }

    void showMenu() {

        System.out.println("Choose operation:");
        System.out.println("    1. add");
        System.out.println("    2. subtract");
        System.out.println("    3. multiply");
        System.out.println("    4. divide");
        System.out.println("    5. square root");
        System.out.print("Choose one (q to quit): ");

    }

    boolean isValid(char ch) {

        if (ch < '1' || ch > '5' && ch != 'q' ) return true;
        else return false;

    }

}

案例3和案例4的println命令始终显示(e,f)和(g,h)以正确的方式捕获,但数学运算始终返回0.任何人都可以看到错误?

2 个答案:

答案 0 :(得分:1)

问题出在副本&amp;糊。

变化:

subResult = operator.multiply(e,f);

为:

mulResult = operator.multiply(e,f);

与案例4相同:

divResult = operator.divide(g,h);
System.out.println(g + " / " + h + " = " + divResult); 

答案 1 :(得分:1)

在案例4中,您仍在调用

operator.multiply(g,h);

我认为你的意思是

operator.divide(g,h);

但是 - 要小心!潜水整数将始终给出整数结果,“真实”答案中的任何小数部分都将被截断,例如1/2 = 0,3 / 2 = 1.