我正在编写一个程序来接收一个正数,并分别输出每个数字。例如,如果输入为692,程序应该输出2、9、6。
我必须使用while循环,并使用模运算符(%)分隔数字。
数字应该是分开的,而不把数字作为字符串,并以数学的方式。
代码:
<div class="column1">
<div class="input">
<button onclick="problem_09()"> Run the program </button>
</div>
<strong><p id="output"> </p></strong>
</div>JAVASCRIPT
function problem_09() {
var outputObj = document.getElementById("output");
var a = parseInt(prompt("Please enter a number: ", ""));
var digits = "";
while(a >= 0){
var d = a % 10;
outputObj.innerHTML= "number: "+a+"<br><br>its digits: " + d;
a = Math.floor(a/10);
}
outputObj.innerHTML = outputObj.innerHTML + "<br><br>" + "program ended";
document.getElementsByTagName("button")[0].setAttribute("disabled","true");
}发布于 2020-10-13 19:34:28
以下是解决办法:
HTML:
<div class="column1">
<div class="input">
<button onclick="problem_09()"> Run the program </button>
</div>
<strong><div id="output"> </div></strong>
</div>联署材料:
function problem_09() {
var outputObj = document.getElementById("output");
var a = parseInt(prompt("Please enter a number: ", ""));
var digit = "";
outputObj.innerHTML = ""
while(a > 0){
let num = a%10
a = Math.floor(a/10)
digit += "<p>"+num+"</p>"
}
outputObj.innerHTML = digit + "<br><br>" + "program ended";
document.getElementsByTagName("button")[0].setAttribute("disabled","true");
}工作代码:https://codepen.io/TheKetan2/pen/rNLOrGR
产出:

发布于 2020-10-13 19:54:05
从数学上讲,取剩余的模10,除以10。
num = some number
while (num>0):
print(num%10)
num = num/10https://stackoverflow.com/questions/64341966
复制相似问题