Java-如何使用while循环解决此计算
我对过去试卷上的一道题有困难。我正在尝试将一个Java-如何使用while循环解决此计算,java,while-loop,Java,While Loop,我对过去试卷上的一道题有困难。我正在尝试将一个从数字乘以一个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 =
从
数字乘以一个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);
}
}
公共类事实{
公共int最后;
私有内部资料部分ND(最终内部资料来源,最终内部资料n){
int fromNum=from;
int toNum=n;
int结果=1;
int c=1;
while(fromNum我认为这应该作为while循环工作
int offset = 0;
int result = fromNum;
while (offset < toNum - fromNum) {
offset++;
result *= fromNum+offset;
}
int offset=0;
int结果=fromNum;
while(偏移量
一些伪代码:
result = from;
temp = from+1;
while(temp <= n) {
result*=temp;
temp++;
}
result=from;
温度=从+1开始;
而(温度intc=0;
int结果=fromNum;
while((fromNum+c)
快速尝试。希望有帮助:-)我将尝试更广泛地回答这个问题,而不仅仅是在循环时专注于。请注意注释:
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);
}
}
请注意,此解决方案基本上有三行。它利用了您的问题分解为一个稍小的问题这一事实。它还优雅地处理您的边缘情况(以及对预期结果的评论)
同样要注意的是,这种方法(一种“递归”方法)会对性能产生影响,但过早优化是万恶之源,正如您第一次尝试时存在清晰度问题一样。这里是您的中心方法的一个简单版本:
private int factPartND(final int from, final int n) {
int f = from;
int result = 1;
while (f <= n) {
result *= f++;
}
return result;
}
简单!你看起来太复杂了。先弄清楚你在纸上是怎么做的,然后编写代码。简单化。我一开始试过了,但我找不到不乘以初始fromNum的方法,因为我在每次迭代中都不断乘以该数字。@ColinD,不,这是过去一篇论文中的问题,我有一个是soon@Colin问题上说这是一张旧试卷?所以不是homework@MattiLyra我以前做过家庭作业,包括修正考试中不正确的问题:p。只是要求确保。这不使用而loop@ColinD确实没有。@nsc010你真的需要在使用的同时使用吗?一个奇怪的要求。@MarkoTopolnik这是一个考试练习。这些都是这样的。顺便说一句,我不认为把它简化成一行对初学者来说是一个好例子。Java代码标准只是一个惯例,但对我来说是一个非常有用的标准。我已经见过太多这样的自由导致的错误了。@Tom我猜你是ri没错。这对我来说是自动的。顺便说一句,我从来没有看到过这样的错误,或者至少没有比任何其他类型的错误更常见。@Tom实际上有一个emacs小调模式,它会使parens变灰:)但在检查语法时,您只需将光标放在打开的paren上,即可立即看到S-expr达到的程度。这非常非常简单,比检查Java语法容易得多。另外,使用paredit
进行编辑就像是一个新世界。这目前是错误的。您的循环检查fromNum
是否,但您从未递增fromNum
。另外,result*fromNum+c
=(result*fromNum)+c
回答得很好。我正要在评论中提到重新声明+1@Nathaniel福特,谢谢你的评论。行:private int factPartND(final int from,final int n)
问题的一部分是我必须使用while循环。但是,我对其余的代码负责。我将检查您的反馈,并尝试在将来的编码中实现它们。感谢您抽出时间发表评论。@nsc010没问题!不过,我建议,即使在学术环境中也要使用常量质疑问题;如果有人给你一个可笑的签名来实现,就大声说出来。如果一些明显的东西可能更好,为什么会这样?最糟糕的情况是他们让你按原样实现,但在所有情况下,人们都意识到你真的在考虑这个问题。@NathanielFordfinal
params,至少实际上是final(可以标记为final
,没有编译错误),实际上是一种广泛推荐的做法。@MarkoTopolnik不要误解我的意思;我不反对它们是final,但前提是它有意义。作者需要理解为什么会是这种情况;否则,您主要只是复制、粘贴和屏蔽该方法的功能。
int result = 1, i = from;
while (i <= to) result *= i++;
System.out.println("Result is " + result);
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);
}
}
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);
}
}
private int factPartND(final int from, final int n) {
int f = from;
int result = 1;
while (f <= n) {
result *= f++;
}
return result;
}
private int factPartND(int from, final int n) {
int result = 1;
while (from <= n) {
result *= from++;
}
return result;
}