<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://beginnersbook.com/2018/09/java-program-to-add-two-binary-numbers/](https://beginnersbook.com/2018/09/java-program-to-add-two-binary-numbers/) 在本教程中,我們將 [java 程序](https://beginnersbook.com/2017/09/java-examples/)編寫為[相加兩個二進制數](https://beginnersbook.com/2018/03/python-program-to-add-two-binary-numbers/)。二進制數系統只有兩個符號 0 和 1 所以二進制數只包含 0 和 1。在我們編寫相加程序之前,讓我們看看我們如何在紙上相加,如下圖所示: ![Adding binary numbers in Java](https://img.kancloud.cn/da/1a/da1a98cc3ba801f0ffc56aaaad71a8f5_541x377.jpg) ## 示例:在 Java 中相加二進制數 在這個程序中,我們使用[`Scanner`](https://beginnersbook.com/2014/07/java-program-to-get-input-from-user/)從用戶獲取輸入(用戶輸入我們需要相加的兩個二進制數),然后我們使用[`while`循環](https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/)逐位相加它們,并將結果存儲在一個數組中。 ```java import java.util.Scanner; public class JavaExample { public static void main(String[] args) { //Two variables to hold two input binary numbers long b1, b2; int i = 0, carry = 0; //This is to hold the output binary number int[] sum = new int[10]; //To read the input binary numbers entered by user Scanner scanner = new Scanner(System.in); //getting first binary number from user System.out.print("Enter first binary number: "); b1 = scanner.nextLong(); //getting second binary number from user System.out.print("Enter second binary number: "); b2 = scanner.nextLong(); //closing scanner after use to avoid memory leak scanner.close(); while (b1 != 0 || b2 != 0) { sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2); carry = (int)((b1 % 10 + b2 % 10 + carry) / 2); b1 = b1 / 10; b2 = b2 / 10; } if (carry != 0) { sum[i++] = carry; } --i; System.out.print("輸出: "); while (i >= 0) { System.out.print(sum[i--]); } System.out.print("\n"); } } ``` 輸出: ```java Enter first binary number: 11100 Enter second binary number: 10101 輸出: 110001 ``` Eclipse IDE 中的相同程序: ![Java - Adding two binary numbers](https://img.kancloud.cn/e5/78/e57840ae74aa1bdd0e6edc4243bb3aab_697x1024.jpg) Eclipse 中程序的輸出: ![Adding binary numbers in Java Output of program](https://img.kancloud.cn/c2/ee/c2ee3a43e96441875b67110e35d8cd41_840x230.jpg) 以下是一些相關的 java 示例: 1. [Java 程序:相加兩個復數](https://beginnersbook.com/2018/09/java-program-to-add-two-complex-numbers/) 2. [Java 程序:相加兩個數字](https://beginnersbook.com/2017/09/java-program-to-add-two-numbers/) 3. [Java 程序:相加數十進制數字](https://beginnersbook.com/2018/06/python-program-add-digits-number/)
                  <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>

                              哎呀哎呀视频在线观看