Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么switch语句会自动回答";“违约”;_Javascript_Switch Statement - Fatal编程技术网

Javascript 为什么switch语句会自动回答";“违约”;

Javascript 为什么switch语句会自动回答";“违约”;,javascript,switch-statement,Javascript,Switch Statement,我试着在家里学习JavaScript,我在做一些基本的程序,在这一次,我试着使用switch语句,但我不明白为什么它会自动回答“default” let note=Number(提示(“将note设置为tu eu au bacalauréat?”); 开关(注){ 病例(注10)和(注12): console.log(“Félications,tu as réussi ton examen avec提及”); 打破 默认值:console.log(“Entre-une-note包含Entre-

我试着在家里学习JavaScript,我在做一些基本的程序,在这一次,我试着使用switch语句,但我不明白为什么它会自动回答“default”

let note=Number(提示(“将note设置为tu eu au bacalauréat?”);
开关(注){
病例(注<10):
console.log(“你是拒绝的,我是安·普罗切恩的机会”);
打破
案例(注=>10)和(注12):
console.log(“Félications,tu as réussi ton examen avec提及”);
打破
默认值:console.log(“Entre-une-note包含Entre-0-et20”)

}
您的
开关
语句实际上没有意义

switch(note)
表示要比较
note
的值,但
case(note<10)
表示要将其与
(note<10)
的结果进行比较

你实际上是在问
note
是否等于
(note<10)
,即
note==(note<10)

你能做的是

switch (note) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
        ...
        break;
    case 10:
    case 11:
    case 12:
        ...
        break;
但正如您所看到的,这变得非常乏味。
开关
不是为基于范围的比较而设计的。在您的情况下,最好的解决方案是只使用
if
/
else if
链。

您需要使用true而不是注意

开关(注意)
->
开关(正确)

因为您对case的条件总是会导致true和false。并且您试图将它与一个与任何case都不匹配的数字进行比较,因此它将直接进入默认值

let note=Number(提示(“将note设置为tu eu au bacalauréat?”);
开关(真){
病例(注<10):
console.log(“你是拒绝的,我是安·普罗切恩的机会”);
打破
案例(注>=10)和(注12):
console.log(“Félications,tu as réussi ton examen avec提及”);
打破
默认值:console.log(“Entre-une-note包含Entre-0-et20”)
}
尝试使用parseInt代替Number

let note=parseInt(提示(“将note改为tu eu au bacalauréat?”);


它可能会解决您的问题

这正是我想要的:

let note = parseInt(prompt("Quel note as tu eu au baccalauréat?"));

if ((note < 0) || (note > 20)) {console.log("Entre une note comprise entre 0 et 20")}
else if (note < 10) {console.log("Tu as raté ton examen")}
else if ((note => 10) && (note <= 12)) {console.log("Félicitations")}
else if ((note > 12) && (note <= 20)) {console.log("Tu as réussi ton examen avec   mention")}
let note=parseInt(提示(“将note改为tu eu au bacalauréat?”);
如果((注意<0)| |(注意>20)){console.log(“Entre-une-note包含Entre-0-et20”)}
else if(注意<10){console.log(“tuas ratéton examen”)}

如果((注=>10)和&(注12)和(请注意,这不是使用
switch
的方式。如果对每种情况都使用类似的比较,则应使用If/else语句。
switch
对表达式求值,然后将结果与特定情况相匹配。嗯,好的,谢谢您的回答,我将尝试使用If/else语句
note=>10
是一个f。)函数,而不是比较。你的意思是
note>=10
?因此,设置
switch(true)
允许代码按预期运行:)@NickParsons是的,但我认为这是对
switch
语句的滥用。:-)是的,我同意,如果station对这类事情更好的话,那不是
Number
的问题,读读其他答案,看看真正的问题是什么。
let note = parseInt(prompt("Quel note as tu eu au baccalauréat?"));

if ((note < 0) || (note > 20)) {console.log("Entre une note comprise entre 0 et 20")}
else if (note < 10) {console.log("Tu as raté ton examen")}
else if ((note => 10) && (note <= 12)) {console.log("Félicitations")}
else if ((note > 12) && (note <= 20)) {console.log("Tu as réussi ton examen avec   mention")}