簡體   English   中英

JS 防御性錯誤處理 try catch while 循環錯誤

[英]JS Defensive Error Handling try catch while loop error

我正在javascript中編寫學習防御性錯誤處理的代碼。該程序根據用戶的選擇計算距離、時間或速度。 這一點工作正常。

然后我嘗試在提示時間 function 下添加一個 try catch 語句,這成功了。 所以我拿了那個代碼並嘗試將它變成一個 function 而不是輸入三次。 這個想法是,如果用戶輸入的不是數字或空白,程序將一直要求輸入數字,直到他們輸入為止。

但是現在每當我輸入不是數字的東西時,while 循環都不會顯示提示並無限循環。 我需要一個 while 循環,因為使用 if 語句會使程序 go 在沒有正確的 output 的情況下運行。

我不確定為什么會這樣。

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!')
}

更改:將 promptNumber function 從“輸入”重新定義為不同的名稱(“變量”)

添加引用變量以獲取所需計算的名稱(時間、速度或距離)

請注意,我嘗試調用“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!')
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM