<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 程序:使用`For`和`While`循環查找階乘 > 原文: [https://beginnersbook.com/2017/09/java-program-to-find-factorial-using-for-and-while-loop/](https://beginnersbook.com/2017/09/java-program-to-find-factorial-using-for-and-while-loop/) 我們將編寫三個 java 程序來查找數字的階乘。 1)使用`for`循環 2)使用`while`循環 3)找到用戶輸入的數字的階乘。在完成程序之前,讓我們理解什么是階乘:數字`n`的因子表示為`n!`,`n!`的值表示為:`1 * 2 * 3 * ... * (n-1) * n` 我們在程序中使用循環實現了相同的邏輯。要了解這些程序,您應該具備以下 [java 教程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)主題的基本知識: * [Java `For`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) * [Java `while`循環](https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/) ## 示例:使用`for`循環查找階乘 ```java public class JavaExample { public static void main(String[] args) { //We will find the factorial of this number int number = 5; long fact = 1; for(int i = 1; i <= number; i++) { fact = fact * i; } System.out.println("Factorial of "+number+" is: "+fact); } } ``` **輸出:** ```java Factorial of 5 is: 120 ``` ## 示例 2:使用`while`循環查找階乘 ```java public class JavaExample { public static void main(String[] args) { //We will find the factorial of this number int number = 5; long fact = 1; int i = 1; while(i<=number) { fact = fact * i; i++; } System.out.println("Factorial of "+number+" is: "+fact); } } ``` **輸出:** ```java Factorial of 5 is: 120 ``` ## 示例 3:查找用戶輸入的數字的階乘 程序使用`while`循環查找輸入數的階乘。 ```java import java.util.Scanner; public class JavaExample { public static void main(String[] args) { //We will find the factorial of this number int number; System.out.println("Enter the number: "); Scanner scanner = new Scanner(System.in); number = scanner.nextInt(); scanner.close(); long fact = 1; int i = 1; while(i<=number) { fact = fact * i; i++; } System.out.println("Factorial of "+number+" is: "+fact); } } ``` 輸出: ```java Enter the number: 6 Factorial of 6 is: 720 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看