【问题标题】:Binary conversion program二进制转换程序
【发布时间】:2020-03-19 14:29:48
【问题描述】:

我正在尝试制作一个程序,将十进制数转换为最多 10 位的二进制数,但我无法弄清楚我当前的代码有什么问题。我对编程很陌生,所以我可能没有学过一些小东西。任何帮助表示赞赏,谢谢。

HTML 正文:

<!DOCTYPE html>
<html>
<head>
    <title>Binary converter</title>
    <script src="binaryConverter.js" type="text/javascript"></script>
</head>

<body>

    <div>

        <div>

            <table>
                <tr>
                    <td>Insert a decimal number to be converted to binary: </td>
                    <td>
                        <input type="text" id="decimal" placeholder="e.g. 54"></input>
                    </td>
                    <td>
                        <button type="button" onClick="calc()">Convert</button>
                    </td>
                </tr>
                <tr>
                    <td>Your result: </td>
                    <td id="resultCell"></td>
                    <td></td>
                </tr>
            </table>

        </div>

    </div>

</body>

</html>

JavaScript 正文:

function calc() {

    var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,];
    var decimal = document.getElementById("decimal").value;

    result[9] == decimal % 2;
    decimal == result[9];

    result[8] == decimal % 2;
    decimal == result[8];

    result[7] == decimal % 2;
    decimal == result[7];

    result[6] == decimal % 2;
    decimal == result[6];

    result[5] == decimal % 2;
    decimal == result[5];

    result[4] == decimal % 2;
    decimal == result[4];

    result[3] == decimal % 2;
    decimal == result[3];

    result[2] == decimal % 2;
    decimal == result[2];

    result[1] == decimal % 2;
    decimal == result[1];

    result[0] == decimal % 2;
    decimal == result[0];

    document.getElementById("resultCell").innerHTML = 
    result[9].toString() +
    result[8].toString() +
    result[7].toString() +
    result[6].toString() +
    result[5].toString() +
    result[4].toString() +
    result[3].toString() +
    result[2].toString() +
    result[1].toString() +
    result[0].toString();
}
    ```

【问题讨论】:

  • 您在result 数组的每个元素中存储相同的数字。
  • 你实际上没有。你只比较这些数字。 (== 比较,= 分配)
  • 我试图做的是每次对其执行算术运算时都会覆盖“十进制”的值

标签: javascript math binary


【解决方案1】:

您可能希望单独读取每个二进制数字。您可以使用按位运算来实现这一点:

  • &gt;&gt; 向右移动一个数字:0b1000 &gt;&gt; 2 === 0b0010
  • &amp; 按位执行:(0b1001 &amp; 0b1110) === 0b1000

使用这些运算符,您可以实现您的算法。

% 运算符将一个数除以另一个数并返回余数:15 % 2 === 11 % 2 === 1

【讨论】:

    【解决方案2】:

    对于那些希望使用内置 Javascript 函数将十进制转换为二进制的用户,只需使用 toString(2)。见https://www.w3schools.com/jsref/jsref_tostring_number.asp

    console.log( (15).toString(2) )
    console.log( (65536).toString(2) )

    它甚至可以与 BigInt 一起使用(尽管目前 BigInt 并未在 Microsoft 浏览器中原生实现)。

    console.log( (123456789012345678901234567890n).toString(2) )

    【讨论】:

      【解决方案3】:

      == 运算符用于比较而不是赋值,您应该改用=

      你也可以用循环来缩短它:

      function calc() {
      
          var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,];
          var decimal = document.getElementById("decimal").value;
      
          for(let i=result.length-1; i>=0; i--){
              result[i] = decimal % 2;
              decimal = Math.floor(decimal / 2);
          }
          document.getElementById("resultCell").innerHTML = result.join('');
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 2011-09-14
        • 2022-01-01
        • 1970-01-01
        • 2021-07-29
        • 1970-01-01
        • 2020-09-02
        相关资源
        最近更新 更多