我正在使用Javascript,并想知道如何将其转换为循环。所以if语句只允许一次传递。我想把它改成一个循环,这样如果我继续输入一个高于8的数字,我的程序将继续重新运行提示。在if语句中,在第一次尝试错误后,我的程序显示: 输入的值太大 值n = 9 A [9]的值=未定义 我想这样做,以便它一直让你运行程序,直到你输入数字1-8,因为这是我有多少变量。 这是编码:
var A = [1,12,23,34,45,56,67,78], maxlen = A.length;
var n = prompt("What element number do u want to access");
if (n >= 0 && n < A.length)
{
document.write("Value of n = ", n, "<br \>", "Value of A[", n, "] = ", A[n]);
}
else {
document.write("The value entered is too large <br \>");
n = prompt("Max length is "+maxlen+"\n What element number do you want to access");
document.write("Value of n = ", n, "<br \>", "Value of A[", n, "] = ", A[n]);