提问人:Vermillion Orange 提问时间:1/17/2023 更新时间:1/17/2023 访问量:69
JS 防御性错误处理 try catch while 循环错误
JS Defensive Error Handling try catch while loop error
问:
我正在编写代码来学习 javascript 中的防御性错误处理。 该程序根据用户的选择计算距离、时间或速度。这个位工作正常。
然后,我尝试在prompt-time函数下添加try catch语句,这奏效了。 因此,我利用了这段代码,并尝试将其变成一个函数,而不是输入三次。 这个想法是,如果用户输入的内容不是数字或空白,程序将继续要求输入数字,直到他们这样做。
但是现在,每当我输入不是数字的内容时,while 循环都不会显示提示并无限循环。我需要一个while循环,因为使用if语句会使程序在没有正确输出的情况下继续运行。
我不确定为什么会这样。
ask = prompt('Calculate distance(d), time (t) or speed (s)')
function notNumber(input) {
while (true)
try {
let input = Number(prompt('Enter your ' + input))
if (isNaN(input) == true || input == "") {
throw "Invalid Input";
}
return input;
}
catch (err) {
console.log(err);
}
}
function promptTime(time) {
time = Number(prompt('Enter your Time'))
if (isNaN(time)) {
notNumber(time)
}
return time;
}
function promptSpeed(speed) {
speed = Number(prompt('Enter your Speed'))
if (isNaN(speed)) {
notNumber(speed)
}
return speed;
}
function promptDistance(distance) {
distance = Number(prompt('Enter your distance'))
if (isNaN(distance)) {
notNumber(distance)
}
return distance;
}
if (ask == 'd') {
let time = promptTime()
let speed = promptSpeed()
distance = time * speed
if (distance == Number) {
console.log('Your distance is ' + distance)
}
if(isNaN(distance)) {
notNumber(distance)
}
}
else if (ask == 't') {
let distance = promptDistance()
let speed = promptSpeed()
time = distance / speed
console.log('Your time is ' + time)
if(isNaN(time)) {
notNumber(time)
}
}
else if (ask == 's') {
let distance = promptDistance()
let time = promptTime()
speed = distance / time
console.log('Your speed is ' + speed)
if(isNaN(speed)) {
notNumber(speed)
}
}
else {
console.log('Please enter a measurement!')
}
答:
0赞
Vermillion Orange
1/17/2023
#1
变化: 将 promptNumber 函数从“input”重新定义为不同的名称(“variable”)
添加了引用变量以获取所需计算的名称(时间、速度或距离)
请注意,我尝试将“refer”称为“name”。但是名称被弃用了。
删除了嵌套的 if 语句,因为它们现在是多余的。例如:
else if (ask == 't') {
let distance = promptDistance()
let speed = promptSpeed()
time = distance / speed
console.log('Your time is ' + time)
/* Got rid of statements like these
if(isNaN(time)) {
notNumber(time)
}
*/
}
最终代码为:
ask = prompt('Calculate distance(d), time (t) or speed (s)')
function promptNumber(variable) {
while (true) {
try {
let input = Number(prompt(`Enter a number for ${refer}`));
if (input == "" || isNaN(input)) {
throw "Invalid Input";
}
return Number(input);
} catch (err) {
console.log(err);
}
}
}
function promptTime(time) {
time = Number(prompt('Enter your Time'))
refer = 'time'
if (isNaN(time)) {
time = promptNumber(time)
}
else {
return time;
}
return time;
}
function promptSpeed(speed) {
speed = Number(prompt('Enter your Speed'))
refer = 'speed'
if (isNaN(speed)) {
speed = promptNumber(speed)
}
else {
return speed;
}
return speed;
}
function promptDistance(distance) {
distance = Number(prompt('Enter your distance'))
refer = 'distance'
if (isNaN(distance)) {
distance = promptNumber(distance)
}
else {
return distance;
}
return distance;
}
let refer = ''
if (ask == 'd') {
let time = promptTime()
let speed = promptSpeed()
distance = time * speed
console.log('Your distance is ' + distance)
}
else if (ask == 't') {
let distance = promptDistance()
let speed = promptSpeed()
time = distance / speed
console.log('Your time is ' + time)
}
else if (ask == 's') {
let distance = promptDistance()
let time = promptTime()
speed = distance / time
console.log('Your speed is ' + speed)
}
else {
console.log('Please enter a measurement!')
}
评论