<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 程序:查找自然數之和 > 原文: [https://beginnersbook.com/2017/09/java-program-to-find-sum-of-natural-numbers/](https://beginnersbook.com/2017/09/java-program-to-find-sum-of-natural-numbers/) 正整數`1,2,3,4`等被稱為自然數。在這里,我們將看到三個程序來計算和顯示自然數的總和。 * 第一個程序使用`while`循環計算總和 * 第二個程序使用`for`循環計算總和 * 第三個程序取`n`的值(由用戶輸入)并計算`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/) ## 示例 1:使用`while`循環查找自然數之和的程序 ```java public class Demo { public static void main(String[] args) { int num = 10, count = 1, total = 0; while(count <= num) { total = total + count; count++; } System.out.println("Sum of first 10 natural numbers is: "+total); } } ``` **輸出:** ```java Sum of first 10 natural numbers is: 55 ``` ## 示例 2:使用`for`循環計算自然數之和的程序 ```java public class Demo { public static void main(String[] args) { int num = 10, count, total = 0; for(count = 1; count <= num; count++){ total = total + count; } System.out.println("Sum of first 10 natural numbers is: "+total); } } ``` 輸出: ```java Sum of first 10 natural numbers is: 55 ``` ## 示例 3:用于查找前`n`個(由用戶輸入)自然數之和的程序 ```java import java.util.Scanner; public class Demo { public static void main(String[] args) { int num, count, total = 0; System.out.println("Enter the value of n:"); //Scanner is used for reading user input Scanner scan = new Scanner(System.in); //nextInt() method reads integer entered by user num = scan.nextInt(); //closing scanner after use scan.close(); for(count = 1; count <= num; count++){ total = total + count; } System.out.println("Sum of first "+num+" natural numbers is: "+total); } } ``` **輸出:** ```java Enter the value of n: 20 Sum of first 20 natural numbers is: 210 ```
                  <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>

                              哎呀哎呀视频在线观看