给定n个数字,显示最高和最低数字

时间:2018-03-14 23:14:19

标签: javascript numbers

我刚刚开始编程并且我一直在尝试解决这个难题,但遗憾的是我找不到任何与此相关的答案,所以这里有:

1)给定n个数字(使用提示),显示该组的最高和最低数字。

var highest = 0, **lowest = 0**;

while(true) {
var num = prompt("Input a number");

  if(num == 0) {break;}

  if(num < 0) {
   alert("Input a valid number.");
  }

  if(num > highest) {
    highest = num;
  }

  **if(num < lowest) {
    lowest = num**

  }

}
document.write("The highest number is: " + highest + "<br>");
document.write("The lowest number is: " + lowest);

我知道代码是错的,可能是变量最低的,所以如果有人帮助我,我会非常感激。

5 个答案:

答案 0 :(得分:3)

每次声明新变量时都需要使用关键字(varletconst),因此需要var lower = 0并且您需要分隔你的两个声明的半冒号。你的下面的星号也打破了If语句。以下应该有效:

let highest = 0; let lowest = 999999;

while(true) {
let num = prompt("Input a number");

if(num == 0) {break;}

if(num < 0) {
  alert("Input a valid number.");
}

if(num > highest) {
  highest = num;
}

if(num < lowest) {
  lowest = num;
}
}
document.write("The highest number is: " + highest + "<br>");
document.write("The lowest number is: " + lowest);

此外,lowest不能设置为0,因为您的数字永远不会低于程序中的数字。我把它设置为99999,但你需要使用你的上限或null

此外,请考虑研究varletconst之间的区别。

答案 1 :(得分:1)

一个想法是将最低值设置为大数,但我认为更好的想法是将它们保留为未定义。因为如果用户在第一次输入时键入0,则表明存在这样的状态。 / p>

下面是您通过一些调整修改的示例代码,以及额外的健全性检查。

&#13;
&#13;
var highest, lowest;

while(true) {
  var num = parseInt(prompt("Input a number, 0 - when done"), 10);

  if(num < 0 || isNaN(num)) {
    alert("Input a valid number.");
    continue;
  }
  
  if(num === 0) {break;}

  if(highest === undefined || num > highest) {
    highest = num;
  }

  if(lowest === undefined || num < lowest) {
    lowest = num;
  }
}

if (lowest === undefined) {
  document.write("No Numbers given");
} else {
  document.write("The highest number is: " + highest + "<br>");
  document.write("The lowest number is: " + lowest);
}
&#13;
&#13;
&#13;

或者,您可以让用户将输入字段留空以查看结果:

&#13;
&#13;
var highest,lowest;

while (true) {
  var num = parseInt(prompt("Input a number.  Leave empty to see results"));
  
  if (isNaN(num)) {break;}
  else if (num < 0) {alert("Negative values are not allowed");}
  else {
    if (num>highest || highest==undefined) {highest = num;}
    if (num<lowest || lowest==undefined) {lowest = num;}
  }
}
document.write("The highest number is: "+(highest!=undefined?highest:"-")+"<br>");
document.write("The lowest number is: "+(lowest!=undefined?lowest:"-"));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可能希望将lowest设置为最开始的可能数字,然后还将提示解析为整数,并确保它是有效数字:

var highest = 0, lowest = Number.MAX_VALUE;
while(true) {
    var num = parseInt(prompt("Input a number"));
    if(num == 0) {
        break;
    }
    if(num < 0 || isNaN(num)) {
        alert("Input a valid number.");
        continue;
    }
    if(num > highest) {
        highest = num;
    }
    if(num < lowest) {
        lowest = num;
    }
}
document.write("The highest number is: " + highest + "<br>");
document.write("The lowest number is: " + lowest);

答案 3 :(得分:0)

  • 您需要将每个输入的值转换为数字,因此您可以使用对象Number从不使用parseInt,因为此4hhrh将转换为4
  • 要避免使用字母等无效值,请使用函数isNaN
  • 如果输入的值无效,请执行语句continue
  • 此语句获得最低值lowest = num < lowest ? num : lowest;

&#13;
&#13;
var highest = 0,
    lowest = Number.MAX_VALUE;

while (true) {
  var num = Number(prompt("Input a number"));

  if (num == 0) {
    break;
  }

  if (isNaN(num) || num < 0) {
    alert("Input a valid number.");
    continue
  }
  
    lowest = num < lowest ? num : lowest;
  
  if (num > highest) {
    highest = num;    
  }  

}
document.write("The highest number is: " + highest + "<br>");
document.write("The lowest number is: " + lowest);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

由于您使用零结束,因此只需在与highestlowest进行比较之前添加一个零检查。您还应该将字符串中的值解析为数字:

var highest = 0, lowest = 0;

while (true) {
  var num = parseInt(prompt("Input a number, 0 - when done"), 10);

  if (num == 0) {
    break;
  }
  if (num < 0 || isNaN(num)) {
    alert("Input a valid number.");
    continue;
  }

  if (highest == 0 || num > highest) {
    highest = num;
  }

  if (lowest == 0 || num < lowest) {
    lowest = num;
  }

}
document.write("The highest number is: " + highest + "<br>");
document.write("The lowest number is: " + lowest);