Java - 如何使用while循环解决此计算

时间:2012-08-24 18:07:23

标签: java while-loop

我对过去的试卷中的问题感到困难。我正在尝试将from数字乘以n个数字。换句话说:from *(from + 1)(from + 2)... * n。

我需要使用while循环来解决这个问题。到目前为止我已经这样做了,不知道该怎么做。我知道代码错了,但已经卡住了一段时间。

public class Fact {

    public int last;

    private int factPartND(final int from, final int n) {
        int fromNum = from;
        int toNum = n;
        int result = 1;
        int c = 1;

        while (fromNum <= toNum) {  // e.g.5*6*7*8*9*10*11
            result = (fromNum) * (fromNum + c);  // calculate 5*6
            final int temp = result;  // store 5*6
            int result1 = temp * (fromNum + c);  // store 5*6*7*....
            c++;  // increments the fromNum in the while code
            fromNum++;  // increments 5 to 11 in the while condition
            last = result1;
        }
        return last;
    }

    public static void main(String[] args) {
        Fact f = new Fact();
        System.out.println(test);
    }
}

6 个答案:

答案 0 :(得分:5)

我认为这应该作为while循环

int offset = 0;
int result = fromNum;
while (offset < toNum - fromNum) {
  offset++;
  result *= fromNum+offset;
}

答案 1 :(得分:4)

int result = 1;
for (int i = from; i <= to; i++) result *= i;
System.out.println("Result is " + result);

严格while

int result = 1, i = from;
while (i <= to) result *= i++;
System.out.println("Result is " + result);

答案 2 :(得分:2)

我将尝试更广泛地回答这个问题,而不是只关注你的while循环。请注意评论:

public class Fact {//I assume, based on your question, you really mean 'Factorial'.
    //Examining this for the first time I might assume that this object has to do with 
    //well-established observations, or 'Facts'. Fight the urge to abbreviate everything.

    public int last;//Why is this a member variable of the class?

    private int factPartND(final int from, final int n) {
        //How are your 'from' and 'n' variables related? It's unclear based on their names.
        //The method name is also incomprehensible.
        //Why are the parameters declared 'final'?
        //Why is this a private method?
        //Why is this not a static method?

        int fromNum = from;//If you're redeclaring, there is probably a problem.
        int toNum = n;
        int result = 1;//Is this your default result? You should be notating it in the method
            //comments if you're assuming some things, like no negative numbers.
        int c = 1;//What is c?


        //You have latched on to 'while' as the only way of doing this.
        while (fromNum <= toNum) {  // e.g.5*6*7*8*9*10*11
            result = (fromNum) * (fromNum + c);  // calculate 5*6
               //And then set result to the result? What about what was in there before?
            final int temp = result;  // store 5*6
               //Why is this int final?
            int result1 = temp * (fromNum + c);  // store 5*6*7*....
            c++;  // increments the fromNum in the while code
               //Actually increments the adder to what you're multiplying by three lines earlier
            fromNum++;  // increments 5 to 11 in the while condition
            last = result1;
               //Your use of temporary variables is way overdone and confusing.
        }
        return last;
    }

    public static void main(String[] args) {
        Fact f = new Fact();
        System.out.println(test);
    }
}

考虑一下,你想写一些返回东西的EXPRESSIONS,而不是写一个做某事的STATEMENTS函数。

public class Factorial {

  /** 
   *  Calculates the product of a series of integers from 'start' to 'end'. 'start' must be
   *  less than or equal to 'end', or it will return 1.
   */ 
  public static factorialRange(int start, int end) {
    if (start > end) { return 1; }

    if (start = end) { return end; }

    return start * factorialRange(start + 1, end);
  }

}

请注意,此解决方案基本上是三行长。它利用了这样一个事实,即你的问题会分解成一个稍小的问题。它还可以优雅地处理您的边缘情况(以及对预期结果的评论)。

另请注意,此方法存在性能影响(“递归”方法),但过早优化是所有邪恶的根源,就像第一次尝试时存在清晰度问题一样。

答案 3 :(得分:0)

一些伪代码:

result = from;
temp = from+1;
while(temp <= n) {
  result*=temp;
  temp++;
}

答案 4 :(得分:0)

int c = 0;

int result = fromNum;

while ((fromNum+c) < toNum ) {
  c++;
  result = result*(fromNum+c);
}

return result;

试试这个快点..希望它有所帮助: - )

答案 5 :(得分:0)

以下是您的核心方法的简单版本:

private int factPartND(final int from, final int n) {
    int f = from;
    int result = 1;

    while (f <= n) {
        result *= f++;
    }
    return result;
}

如果您删除final参数上的from修饰符,则可以删除局部变量f

private int factPartND(int from, final int n) {
    int result = 1;

    while (from <= n) {
        result *= from++;
    }
    return result;
}

简单!