简体   繁体   English

Java制作数学方法

[英]Java making maths methods

I'm creating some method which do simple maths calculations. 我正在创建一些进行简单数学计算的方法。 I have a square, cube and hypercube method. 我有一个正方形,立方体和超立方体方法。 And I'm trying to create a power method which when n = 2, calls the square method, when n = 3, calls the cube method etc. I want this power method to return an int k. 我正在尝试创建一个幂方法,当n = 2时,调用方方法,当n = 3时,调用多维数据集方法,等等。我希望此power方法返回int k。 However the value of k doesn't seem to be travelling behond the if statements. 但是,k的值似乎不在if语句之内。 Any suggestions? 有什么建议么?

  public int power(int x, int n){
    int k;
    if (n==2){
        k = square(x);
    }
    else if (n==3){
        k = cube(x);
    }
    else if (n==4){
        k = hypercube(x);
    }
    else if (n==1){
        k = x;
    }
    return k;
}

With your code, I get from the compiler "error: variable k might not have been initialized". 使用您的代码,我从编译器中得到“错误:变量k可能尚未初始化”。 I'm guessing this is what you mean. 我猜这就是你的意思。 This is because, like the error suggests, if n isn't in the range 1 - 4, k is never explicitly set to any value. 这是因为,如错误所示,如果n不在1-4范围内,则k永远不会显式设置为任何值。 Here's one way to fix the problem, which allows the method to work for larger values of n (as long as you don't loop past Integer.MAX_VALUE): 这是解决问题的一种方法,它使该方法适用于较大的n值(只要您不循环经过Integer.MAX_VALUE)即可:

public int power(int x, int n){
    int k;
    if (n==2){
        k = square(x);
    }
    else if (n==3){
        k = cube(x);
    }
    else if (n==4){
        k = hypercube(x);
    }
    else if (n==1){
        k = x;
    }
    else {
        k = 1;
        for (int i = 0; i < n; i++) {
            k *= x;
        }
    }
    return k;
}

You can use a loop for this, like here (I hope I got it right). 您可以为此使用循环,例如此处(我希望我做对了)。

public int power(int x, int n){
    if(n==0) return 1;
    int k = x;
    while(--n > 0) {
        k *= x;
    }
    return k;
}

You might also add support for negative powers, but then you'd need some other return type (eg. float or double). 您可能还添加了对负幂的支持,但是随后您需要其他一些返回类型(例如float或double)。

But honestly, why don't you just use Math.pow() ? 但老实说,您为什么不只使用Math.pow()呢?

This code does not compile since k has not been initialized and you get compilation error at return k; 由于尚未初始化return k;并且return k;出现编译错误,因此该代码无法编译return k; . if you want to initialize with 0, use int k=0 in the declaration 如果要使用0初始化,请在声明中使用int k=0

k will be "known" by the end of the method if it was assigned along the way. k是沿途分配的,则k在方法结束时将是“已知的”。 Perhaps you enforce that no invalid value of k can be passed in, then it should do what you want it to do: 也许您强制执行k不能传递任何无效值,然后它应该执行您想要的操作:

public int power(int x, int n){
    if (n < 1 || n > 4){
        throw new IllegalArgumentException("n is invalid :" + n);
    }
    int k = 0;
    if (n==2){
        k = square(x);
    }
    else if (n==3){
        k = cube(x);
    }
    else if (n==4){
        k = hypercube(x);
    }
    else if (n==1){
        k = x;
    }
    return k;
}

You should also make sure that x is always valid. 您还应确保x始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM