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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 程序:使用數組計算平均值 > 原文: [https://beginnersbook.com/2017/09/java-program-to-calculate-average-using-array/](https://beginnersbook.com/2017/09/java-program-to-calculate-average-using-array/) 我們將看到兩個使用數組查找數字平均值的程序。第一個程序查找指定數組元素的平均值。第二個程序獲取`n`(元素數)的值和用戶提供的數字,并使用數組查找它們的平均值。 要了解這些程序,您應該具備以下 [Java 編程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)概念的知識: 1) [Java 數組](https://beginnersbook.com/2013/05/java-arrays/) 2) [`For`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) ## 示例 1:使用數組查找數字平均值 ```java public class JavaExample { public static void main(String[] args) { double[] arr = {19, 12.89, 16.5, 200, 13.7}; double total = 0; for(int i=0; i<arr.length; i++){ total = total + arr[i]; } /* arr.length returns the number of elements * present in the array */ double average = total / arr.length; /* This is used for displaying the formatted output * if you give %.4f then the output would have 4 digits * after decimal point. */ System.out.format("The average is: %.3f", average); } } ``` 輸出: ```java The average is: 52.418 ``` ## 示例 2:計算用戶輸入的數字的平均值 在此示例中,我們使用`Scanner`來獲取`n`的值以及來自用戶的所有數字。 ```java import java.util.Scanner; public class JavaExample { public static void main(String[] args) { System.out.println("How many numbers you want to enter?"); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); /* Declaring array of n elements, the value * of n is provided by the user */ double[] arr = new double[n]; double total = 0; for(int i=0; i<arr.length; i++){ System.out.print("Enter Element No."+(i+1)+": "); arr[i] = scanner.nextDouble(); } scanner.close(); for(int i=0; i<arr.length; i++){ total = total + arr[i]; } double average = total / arr.length; System.out.format("The average is: %.3f", average); } } ``` **輸出:** ```java How many numbers you want to enter? 5 Enter Element No.1: 12.7 Enter Element No.2: 18.9 Enter Element No.3: 20 Enter Element No.4: 13.923 Enter Element No.5: 15.6 The average is: 16.225 ```
                  <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>

                              哎呀哎呀视频在线观看