在 if 语句中处理变量时如何忽略大小写 - Javascript

问题描述

我是 Javascript 的完全初学者,并尝试使用尽可能简单的代码。在 if 语句中使用提示中的变量时,在区分大小写方面存在一些问题。这是有问题的代码。假设输入完全是“法语”,它可以完美运行

if (number => 1 && number <= 30) {
    var lang= prompt ("Which language do you want to translate into,french or German?");
}
while(lang != "french" && lang != "french" && lang != "french" && lang != "German" && lang != "german" && lang != "GERMAN" ) {
    alert("Only french or German is allowed");
    var lang= prompt ("Which language do you want to translate into,french or German?");
}   


    if (number == 1 && lang == "french") {
        alert("The translation is " +frenchNumbers[1]);
    }
    if (number == 2 && lang == "french") {
        alert("The translation is " +frenchNumbers[2]);
    }
    if (number == 3 && lang == "french") {
        alert("The translation is " +frenchNumbers[3]);
    }

如果用户要输入 'french' 或 'french',我希望 'if' 语句也能工作。我尝试使用下一段代码,但它从一开始就循环显示警报,这不是我想要的。例如,如果我要输入“3”和“french”,我将首先收到“1”和“2”的警报。

if (number == 1 && lang == "french" || lang == "french" || lang == "french") {
        alert("The translation is " +frenchNumbers[1]);
    }
    if (number == 2 && lang == "french" || lang == "french" || lang == "french") {
        alert("The translation is " +frenchNumbers[2]);
    }
    if (number == 3 && lang == "french" || lang == "french" || lang == "french") {
        alert("The translation is " +frenchNumbers[3]);
    }

我相信有一个简单的解决方法,尝试解决它有点疯狂,因为我查看的所有信息都远高于我的技能水平

任何帮助都会很棒

谢谢,布拉德

解决方法

正如 Chris Baker 所述,您可以使用 public class Product extends ConsoleProgram{ public void run(){ int intValue = 5; double doubleValue = 2.5; int product1 = product(intValue,intValue); System.out.println(product1); // Use method overloading to define methods // for each of the following method calls double product2 = product(doubleValue,doubleValue); System.out.println(product2); int product3 = product(intValue,intValue,intValue); System.out.println(product3); double product4 = product(intValue,doubleValue); System.out.println(product4); double product5 = product(doubleValue,intValue); System.out.println(product5); double product6 = product(doubleValue,doubleValue,doubleValue); System.out.println(product6); } public int product(int one,int two){ return (int)(one * two); } public double product(double one,double two){ return (double)(one * two); } public double product(int one,double two){ return (double)(one * two); } public int product(int one,int two,int three){ return (int)(one * two * three); } public double product(double one,double two,double three){ return (double)(one * two * three); } public double product(double one,int two){ return (double)(one * two); } } 。需要调整的代码如下。

.toLowerCase()