<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 程序:相加兩個復數 > 原文: [https://beginnersbook.com/2018/09/java-program-to-add-two-complex-numbers/](https://beginnersbook.com/2018/09/java-program-to-add-two-complex-numbers/) **復數**有兩部分 - 實部??和虛部。在[教程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)中,我們編寫 [Java 程序](https://beginnersbook.com/2017/09/java-examples/)來**相加兩個復數**。相加復數時,我們將實部和虛部加在一起,如下圖所示。 ![Java Add two complex numbers](https://img.kancloud.cn/dd/bc/ddbc8d1b17cf1be49b0d2c7cd3a27d8c_600x249.jpg) ## 示例 - 在 Java 中相加兩個復數 在這個程序中,我們有一個類`ComplexNumber`。在這個類中,我們有兩個實例變量`real`和`img`來保存復數的實部和虛部。 我們已經聲明了一個方法`sum()`,[通過將它們的實部和虛部加在一起來相加兩個數字](https://beginnersbook.com/2017/09/java-program-to-add-two-numbers/)。 此類的構造函數用于初始化復數。對于例如當我們像這個`ComplexNumber temp = new ComplexNumber(0, 0);`創建這個類的實例時,它實際上會創建一個復數`0 + 0i`。 ```java public class ComplexNumber{ //for real and imaginary parts of complex numbers double real, img; //constructor to initialize the complex number ComplexNumber(double r, double i){ this.real = r; this.img = i; } public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) { //creating a temporary complex number to hold the sum of two numbers ComplexNumber temp = new ComplexNumber(0, 0); temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; //returning the output complex number return temp; } public static void main(String args[]) { ComplexNumber c1 = new ComplexNumber(5.5, 4); ComplexNumber c2 = new ComplexNumber(1.2, 3.5); ComplexNumber temp = sum(c1, c2); System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i"); } } ``` **輸出:** ```java Sum is: 6.7 + 7.5i ``` 屏幕截圖:Eclipse IDE 中的相同 Java 程序 - ![Java Program to add two complex numbers in Eclipse IDE](https://img.kancloud.cn/8a/4f/8a4f21bdefb6aa0a3a3c118968907243_1024x910.jpg) Eclipse IDE 中的輸出: ![Output of adding two complex numbers in Java](https://img.kancloud.cn/a8/3a/a83a785b75fb5598ddf51c2789a5a6ce_836x162.jpg) 以下是一些相關的 java 示例: 1. 1. [Java 程序:找到最大的三個數字](https://beginnersbook.com/2017/09/java-program-to-find-largest-of-three-numbers/) 2. [Java 程序:使用`Switch Case`制作計算器](https://beginnersbook.com/2017/09/java-program-to-make-a-calculator-using-switch-case/) 3. [Java 程序:交換兩個數字](https://beginnersbook.com/2017/09/java-program-to-swap-two-numbers-using-bitwise-xor-operator/) 4. [C++ 程序:相加兩個復數](https://beginnersbook.com/2017/12/c-program-to-add-complex-numbers/)
                  <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>

                              哎呀哎呀视频在线观看