promotion image of download ymail app
Promoted

關於寫Javascript 程式問題~

寫一個Javascript程式,可以讓使用者輸入兩個整數值.

以此兩個整數值展示+ , - , * , / , %(取餘數), > , < , == , != ,

>= , <= 等運算子的作用.

P.S ( output 不用 alert , 而用 document.write )

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    <html>

    <head>

    <SCRIPT language=javascript>

    <!--

    function test(form)

    {

    if (isNaN(form.number1.value) || isNaN(form.number2.value))

    alert("輸入錯誤! 要輸入整數阿!");

    else

    {

    var num1, num2, ret;

    num1 = parseInt(form.number1.value);

    num2 = parseInt(form.number2.value);

    document.open();

    ret = num1 + num2;

    document.write(num1 + '+' + num2 + '=' + ret + "<br>");

    ret = num1 - num2;

    document.write(num1 + '-' + num2 + '=' + ret + "<br>");

    ret = num1 * num2;

    document.write(num1 + '*' + num2 + '=' + ret + "<br>");

    ret = num1 / num2;

    document.write(num1 + '/' + num2 + '=' + ret + "<br>");

    ret = num1 % num2;

    document.write(num1 + '%' + num2 + '=' + ret + "<br>");

    if (num1 > num2)

    document.write(num1 + '>' + num2 + " yes" + "<br>");

    else

    document.write(num1 + '>' + num2 + " no" + "<br>");

    if (num1 < num2)

    document.write(num1 + '<' + num2 + " yes" + "<br>");

    else

    document.write(num1 + '<' + num2 + " no" + "<br>");

    if (num1 == num2)

    document.write(num1 + "==" + num2 + " yes" + "<br>");

    else

    document.write(num1 + "==" + num2 + " no" + "<br>");

    if (num1 != num2)

    document.write(num1 + "!=" + num2 + " yes" + "<br>");

    else

    document.write(num1 + "!=" + num2 + " no" + "<br>");

    if (num1 >= num2)

    document.write(num1 + ">=" + num2 + " yes" + "<br>");

    else

    document.write(num1 + ">=" + num2 + " no" + "<br>");

    if (num1 <= num2)

    document.write(num1 + "<=" + num2 + " yes" + "<br>");

    else

    document.write(num1 + "<=" + num2 + " no" + "<br>");

    document.close();

    }

    }

    -->

    </SCRIPT>

    </head>

    <body>

    <form method=post>

    <br>輸入 number1, number2<br>

    <input type=text name="number1">

    <input type=text name="number2">

    <input type=BUTTON onClick="test(this.form)" value="運算" name="BUTTON1">

    </form>

    </body>

    </html>

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.