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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Kotlin 程序:查找二次方程的所有根 > 原文: [https://www.programiz.com/kotlin-programming/examples/quadratic-roots-equation](https://www.programiz.com/kotlin-programming/examples/quadratic-roots-equation) #### 在此程序中,您將學習查找二次方程式的所有根(取決于判別式),并在 Kotlin 中使用`format()`打印它們。 二次方程的標準形式為: ```kt ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0 ``` 術語`b<sup>2</sup>-4ac`被稱為二次方程的判別式。 判別式說明了根的性質。 * 如果判別式大于 0,則根是實數且不同。 * 如果判別式等于 0,則根是實數且相等。 * 如果判別式小于 0,則根是復數且不同。 * * * ## 示例:Kotlin 程序來查找二次方程的根 ```kt fun main(args: Array<String>) { val a = 2.3 val b = 4 val c = 5.6 val root1: Double val root2: Double val output: String val determinant = b * b - 4.0 * a * c // condition for real and different roots if (determinant > 0) { root1 = (-b + Math.sqrt(determinant)) / (2 * a) root2 = (-b - Math.sqrt(determinant)) / (2 * a) output = "root1 = %.2f and root2 = %.2f".format(root1, root2) } // Condition for real and equal roots else if (determinant == 0.0) { root2 = -b / (2 * a) root1 = root2 output = "root1 = root2 = %.2f;".format(root1) } // If roots are not real else { val realPart = -b / (2 * a) val imaginaryPart = Math.sqrt(-determinant) / (2 * a) output = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) } println(output) } ``` 運行該程序時,輸出為: ```kt root1 = -0.87+1.30i and root2 = -0.87-1.30i ``` * * * 在上述程序中,系數`a`,`b`和`c`分別設置為 2.3、4 和 5.6。 然后,將`determinant`計算為`b ^ 2 - 4ac`。 根據判別式的值,根的計算公式如上式所示。 注意,我們已經使用庫函數`Math.sqrt())`計算數字的平方根。 然后使用 Kotlin 的標準庫函數`format()`將要打印的輸出存儲在字符串變量`output`中。 然后使用`println()`打印輸出。 * * * 以下是上述程序的等效 Java 代碼:[用于查找四級方程的所有根的 Java 程序](/java-programming/examples/quadratic-roots-equation)
                  <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>

                              哎呀哎呀视频在线观看