在Javascript中循环提示,直到满足条件
我试图使用while循环来显示提示消息,提示用户输入1到100之间的数字。我希望代码执行一个循环,直到用户输入正确的数字(介于1和100之间),然后执行函数在Javascript中循环提示,直到满足条件,javascript,loops,while-loop,Javascript,Loops,While Loop,我试图使用while循环来显示提示消息,提示用户输入1到100之间的数字。我希望代码执行一个循环,直到用户输入正确的数字(介于1和100之间),然后执行函数createGrid(parseInt(message)) 当我键入一个正确的数字(介于1和100之间)时,我的代码会工作,当数字大于100或小于1时,代码会显示警报,但如果尝试输入另一个介于1和100之间的数字,即使数字正确,代码也会持续显示警报 我的while循环中肯定有错误,但我不知道如何修复它 代码如下: function clear
createGrid(parseInt(message))
当我键入一个正确的数字(介于1和100之间)时,我的代码会工作,当数字大于100或小于1时,代码会显示警报,但如果尝试输入另一个介于1和100之间的数字,即使数字正确,代码也会持续显示警报
我的while循环中肯定有错误,但我不知道如何修复它
代码如下:
function clearGrid() {
while (container.firstChild) {
container.firstChild.remove();
}
let input;
let message = prompt('Enter the number of squares for each side of the new grid:');
if (parseInt(message) < 1 || parseInt(message) > 100) {
input = false;
while (input === false) {
alert('Please enter a valid number between 1 and 100.');
message = prompt('Enter the number of squares for each side of the new grid:');
}
if (parseInt(message) > 1 || parseInt(message) < 100) {
createGrid(parseInt(message));
}
}
函数clearGrid(){
while(container.firstChild){
container.firstChild.remove();
}
让输入;
let message=prompt('输入新网格每侧的方块数:');
if(parseInt(message)<1 | | parseInt(message)>100){
输入=假;
while(输入===false){
警报('请输入一个介于1和100之间的有效数字');
message=prompt('输入新网格每侧的方块数:');
}
如果(parseInt(消息)>1 | | parseInt(消息)<100){
createGrid(parseInt(message));
}
}
首先,你需要设置-¬ | | |如果int大于1,那么它就是好的(意味着它可以从1到1000000000),或者它可以是(100--1000000000),意味着你可以选择所有的数字 还有两个if语句,请先看第一个,然后看其他,看两个if语句做同样的事情有点让人困惑 这就是我要做的
if ( 0 < parseInt(message) < 100) {
createGrid(parseInt(message));
}
else{
input = false;
while (input === false) {
alert('Please enter a valid number between 1 and 100.');
message = prompt('Enter the number of squares for each side of the newgrid:');
} }
if(0
我看不出这个代码还有什么问题,我想你可能没有把所有的代码都发送到这里,所以你可以开始了
编辑if语句中缺少括号?是否没有错误?您的while循环检查
input==false
,循环内部没有可将input
设置为true
,这意味着没有任何东西可停止循环。