我对过去的试卷中的问题感到困难.我正在尝试将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);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这应该作为while循环
int offset = 0;
int result = fromNum;
while (offset < toNum - fromNum) {
offset++;
result *= fromNum+offset;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
770 次 |
| 最近记录: |