簡體   English   中英

來自用戶輸入的多個 x 數字使用 for 循環還是 while 循環?

[英]Multiple x number from user-input using for-loop or while-loop?

我有 2 列表。 左表的第一行顯示標簽“輸入 X(數字)”,並在其旁邊輸入字段類型 =“數字”。 第二行“顯示 X”表示將顯示多少多個 X,第三行表示輸出/結果。

例子:

用戶輸入

  • 輸入 X(number): 10
  • 顯示 X : 7

結果(輸出)

10、20、30、40、50、60、70

如何用 for 循環做到這一點? 如何使用具有相同結果的while循環來做到這一點?

<html>
<head>
<script>
function showValues() {
    var num1 = document.getElementById("input1").value;
    var num2 = document.getElementById("input2").value;
    var outputString = "" + (num1);
    for (var count = 2; count <= num2; count++) {
       outputString += ", " + (num1 * count); 
    }
    document.getElementById("theAnswer").value = outputString;
    }
</script>
</head>
<body>
<div>
<table id="tableid">

<tr>
<td>Enter X(number):</td> <td><input id="input1"></td>
</tr>

<tr>
<td>Show X:</td> <td><input id="input2" ></td>
</tr>

<tr>
<td> answer </td> <td><input id="theAnswer" ></td>
</tr>
</table>
<br/>


<button type="button" onclick="showValues();">showValues<br/>
</div>
</body></html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM