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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 程序:計算數字的冪 > 原文: [https://www.programiz.com/java-programming/examples/power-number](https://www.programiz.com/java-programming/examples/power-number) #### 在此程序中,您將學習使用和不使用`pow()`函數來計算數字的冪。 ## 示例 1:使用`while`循環計算數字的冪 ```java public class Power { public static void main(String[] args) { int base = 3, exponent = 4; long result = 1; while (exponent != 0) { result *= base; --exponent; } System.out.println("Answer = " + result); } } ``` 運行該程序時,輸出為: ```java Answer = 81 ``` 在此程序中,`base`和`exponent`分別分配了值 3 和 4。 使用`while`循環,我們繼續將`result`與`base`相乘,直到`exponent`變為零。 在這種情況下,我們將`result`乘以基數總共 4 倍,因此`result = 1 * 3 * 3 * 3 * 3 = 81`。 * * * ## 示例 2:使用`for`循環計算數字的冪 ```java public class Power { public static void main(String[] args) { int base = 3, exponent = 4; long result = 1; for (;exponent != 0; --exponent) { result *= base; } System.out.println("Answer = " + result); } } ``` 運行該程序時,輸出為: ```java Answer = 81 ``` 在這里,我們使用了`for`循環,而不是使用`while`循環。 每次迭代后,`exponent`減 1,然后`result`乘以`base exponent`次數。 如果您的指數為負,則以上兩個程序均無效。 為此,您需要在 Java 標準庫中使用`pow()`函數。 * * * ## 示例 3:使用`pow()`函數計算數字的冪 ```java public class Power { public static void main(String[] args) { int base = 3, exponent = -4; double result = Math.pow(base, exponent); System.out.println("Answer = " + result); } } ``` 運行該程序時,輸出為: ```java Answer = 0.012345679012345678 ``` 在此程序中,我們使用 Java 的`Math.pow()`函數來計算給定`base`的功率。
                  <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>

                              哎呀哎呀视频在线观看