我不确定如何构造我的算术方法

时间:2012-01-01 07:49:21

标签: java

我是Java的新手。我想要一个使用submethods(?)的方法。我想要这样的东西:

Math.Addition(1, 1, X in this case, integer of users choice for the output);

输出将存储在变量X中。但我也想做这样的事情:

Math.Subtraction(2, 1, X);

我该怎么做?

2 个答案:

答案 0 :(得分:3)

创建一个名为Math的类。

并且在其中有两个方法,使用必需的参数命名为“Addition”和“Subtraction”,并将每个方法的逻辑放在方法中。

public class Math {
public static int X; 
public static int Addition(int a, int b, String choice) {
    X = a+b;
    return X;
}

public static int Substraction(int a, int b, String choice) {
    X = a-b;
    return X;
}

}

答案 1 :(得分:0)

您可能需要返回该值才能分配给x。 e.g。

X = myClass.add(1,1);