2012-08-24 36 views
2

我在過去的考試試卷中遇到問題。我正在嘗試將from號碼乘以n號碼。換句話說:從*(從+ 1)(從+ 2)... * n。Java - 如何使用while循環解決此計算

我需要通過使用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

是這個作業嗎? –

+0

我最初嘗試過,但是我無法找到一種不由inital fromNum乘法的方法,因爲我通過每次迭代繼續乘以該數字。 – nsc010

回答

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); 
+0

這不使用'while'循環 –

+0

@ColinD確實沒有。 @ nsc010你真的需要使用'while'而沒有其他的東西嗎?一個奇怪的要求。 –

+0

@MarkoTopolnik這是一次考試練習。這些傾向於這樣。順便說一句,我不認爲把這個縮小到一條線是初學者的一個好例子。 Java代碼標準只是一個慣例,但是一個非常有用的恕我直言。我看到太多的錯誤導致了這種自由太多的錯誤。 – toniedzwiedz

5

我覺得這應該是while循環

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

一些僞代碼:

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

int result = fromNum; 

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

return result; 

試試這個quicky ..希望這有助於:-)

+1

這是目前錯誤。你的循環會檢查'fromNum

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

考慮,而不是寫那些東西,你要編寫返回的東西表達語句的功能。

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

} 

請注意,該解決方案本質上是三行。它利用了您的問題分解成稍微小一點的問題。它也優雅地處理您的邊緣案例(並對預期結果進行評論)。

請注意,這種方法會有性能影響(「遞歸」方法),但過早優化是所有惡意的根源,就像您的第一次嘗試存在清晰度問題一樣。

+1

好的答案。我正要在評論中提及重新聲明。 +1 – toniedzwiedz

+0

@Nathaniel Ford,感謝您提出您的意見。該行:'private int factPartND(final int from,final int n)'並且不得不使用while循環給我作爲問題的一部分。但是我對其餘的代碼負責。我會仔細閱讀您的反饋意見,並嘗試將其應用於未來的編碼。感謝您抽出時間發表評論。 – nsc010

+1

@ nsc010沒問題!然而,即使在學術環境中,我也會建議不斷提出問題;如果有人給你一個荒謬的簽名來執行,請將其發出。爲什麼這樣,如果明顯的東西可能會更好?最糟糕的情況是他們讓你按原樣實施,但在所有情況下,人們都認識到你真的在考慮這個問題。 –

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

簡單!