Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 2 decades ago

one more question

2 Answers

Rating
  • yup
    Lv 5
    2 decades ago
    Favorite Answer

    <script language="javascript">

    <!--

    window.menubar="no";

    var mainMessage = "Enter a starting number for summing (between -1000 and +1000)";

    window.alert("Welcome to the Number Ranger Program!")

    window.alert("You will enter any two integers between -1000 and 1000.\n This program will display the numbers you selected, \n then provide statistics about the numbers");

    var first = parseInt(window.prompt( mainMessage,"" ));

    while ( isNaN( first ) || Math.abs(first) > 1000)

    first = parseInt(window.prompt("Input error..." + mainMessage,""));

    var second = parseInt(window.prompt( "Enter a stopping number for summing (between -1000 and +1000)",""));

    while ( isNaN( second ) || Math.abs(second) > 1000)

    second = parseInt(window.prompt("Input error..." + mainMessage,"" ));

    document.write("<table border='1'>");

    document.write("<tr><td>The range you selected is in the table below:</td></tr>");

    if( second > first )

    {

    var sum = 0;

    var counter = 0;

    for(i = first; i <= second; i++)

    {

    document.write( "<tr><td>" + i + "</td></tr>");

    sum = sum + i;

    counter++;

    }

    document.write("</table>");

    }

    else

    {

    var sum = 0;

    var counter = 0;

    for (i = first; i >= second; i--)

    {

    document.write( "<tr><td>" + i + "</td></tr>");

    sum = sum + i;

    counter++;

    }

    document.write("</table>");

    }

    document.write("<p><table border='1'>");

    document.write("<tr><td>The sum of these numbers is:</td><td>" + sum + "</td></tr>" );

    document.write("<tr><td>The range of these numbers is:</td><td>" + (Math.abs( second - first) + 1)+ "</td></tr>");

    document.write("<tr><td>The average of these number is: </td><td>" + sum/counter + "</td></tr>");

    document.write("</table>");

    // -->

    </script>

  • 2 decades ago

    點數太少~~ ^_^

    提示:利用for回圈再利用document.write 來寫table

    在迴圈裏面季路統計變數

    最後再寫出來就完成了

Still have questions? Get your answers by asking now.