2

我在过去试卷中的一个问题上遇到了困难。我正在尝试将一个数字乘以一个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);
    }
}
4

6 回答 6

5

我认为这应该作为while循环

int offset = 0;
int result = fromNum;
while (offset < toNum - fromNum) {
  offset++;
  result *= fromNum+offset;
}
于 2012-08-24T18:11:46.513 回答
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);
于 2012-08-24T18:20:29.487 回答
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);
    }
}

考虑一下,而不是编写执行某些操作的 STATEMENTS 函数,您想编写返回事物的 EXPRESSIONS。

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);
  }

}

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

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

于 2012-08-24T18:29:11.670 回答
0

一些伪代码:

result = from;
temp = from+1;
while(temp <= n) {
  result*=temp;
  temp++;
}
于 2012-08-24T18:13:15.967 回答
0
int c = 0;

int result = fromNum;

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

return result;

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

于 2012-08-24T18:15:07.607 回答
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;
}

简单的!

于 2012-08-24T18:36:06.750 回答