我刚刚开始编程并且我一直在尝试解决这个难题,但遗憾的是我找不到任何与此相关的答案,所以这里有:
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);
我知道代码是错的,可能是变量最低的,所以如果有人帮助我,我会非常感激。
答案 0 :(得分:3)
每次声明新变量时都需要使用关键字(var
,let
,const
),因此需要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
。
此外,请考虑研究var
,let
和const
之间的区别。
答案 1 :(得分:1)
一个想法是将最低值设置为大数,但我认为更好的想法是将它们保留为未定义。因为如果用户在第一次输入时键入0,则表明存在这样的状态。 / p>
下面是您通过一些调整修改的示例代码,以及额外的健全性检查。
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;
或者,您可以让用户将输入字段留空以查看结果:
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;
答案 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;
。
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;
答案 4 :(得分:0)
由于您使用零结束,因此只需在与highest
和lowest
进行比较之前添加一个零检查。您还应该将字符串中的值解析为数字:
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);