新增JS,这里是问题,代码要求用户输入任意数量的数字 - 1结束,查找并显示最大和最小;它的工作除了它显示最小的最大,但最大的显示器 罚款?在javascript中使用for循环编写代码,提示用户输入任意数量的数字打印最大和最小的那些数字
var numInputs = parseInt(prompt("Please enter numbers seperated by
a space end with -1"))
var largestNum =0;
var smallestNum = 9999;
if (numInputs >0)
{
for (i=1; i<=numInputs; i++)
{
if (numInputs > largestNum)
{
largestNum = numInputs;
}
}
}
alert("The largest number entered was " + largestNum);
if (numInputs >0)
{
for (i=1; i<=numInputs; i++)
{
if (numInputs < smallestNum)
{
smallestNum = numInputs;
}
}
}
alert("The smallest number entered was " + smallestNum);
*“它的工作除了”* - 我真的不认为它工作。 'parseInt()'不会像你认为的那样工作:如果你给它一串空格分隔的数字,它会返回第一个数字。即使它返回了所有数字的数组,或者是所有数字的数组,您的循环也没有意义,因为您将'numInputs'与'largestNum'变量进行比较;你从不在循环中使用'i'。 – nnnnnn