0

在我回答这个问题之前,我只想说一件事。我是一个对编程感兴趣的年轻人,所以如果答案超级简单,请不要对我发火。PS。对不起我的英语不好:P

事情就是这样。我正在尝试在 JavaScript 中制作华氏温度和摄氏温度之间的转换器。我根本无法理解我enter code here做错了什么。我的代码;

//Line 2 asks the user what he/she want to convert. Either celcius to farenheit, or farenheit to celcius
var userChoice = prompt("For converting farenheit to celcius, type FtoC. For converting celcius to farenheit, type CtoF");


//Defines a function to calculate the math
var celciustofarenheit = function (userChoice) {
    if (userChoice === "CtoF")
        var CtoFchoice = prompt("Whats the degree you want to convert?")
            var result1 = CtoFchoice * 9 / 5 + 32;
                console.log(CtoFchoice, "is", result1, "in farenheit!")
}
    if (userChoice === "FtoC") {
        var FtoCchoice = prompt("Whats the number you want to convert?")
            var result2 = FtoCchoice - 32 * 5 / 9;
                console.log(FtoCchoice, "is", result2, "in celcius!")
    }
};

请告诉我我做错了什么,而不仅仅是粘贴正确的代码!:)

4

2 回答 2

2

您在 . 之后缺少一个花括号if (userChoice === "CtoF")。如果您正确缩进代码,您将直接发现此类错误。

于 2013-04-19T19:41:01.620 回答
0

正确的代码

//Defines a function to calculate the math
var celciustofarenheit = function (userChoice) {
    if (userChoice === "CtoF") {
        var CtoFchoice = prompt("Whats the degree you want to convert?");
        var result1 = CtoFchoice * 9 / 5 + 32;
        console.log(CtoFchoice, "is", result1, "in farenheit!");
    }

    if (userChoice === "FtoC") {
        var FtoCchoice = prompt("Whats the number you want to convert?");
        var result2 = FtoCchoice - 32 * 5 / 9;
        console.log(FtoCchoice, "is", result2, "in celcius!");
    }
};
于 2013-04-19T19:42:43.063 回答